Coder Social home page Coder Social logo

Custom tags via AddMvc? about fluid HOT 12 CLOSED

sebastienros avatar sebastienros commented on August 15, 2024 1
Custom tags via AddMvc?

from fluid.

Comments (12)

sebastienros avatar sebastienros commented on August 15, 2024 1

You can change the default template type which is FluidTemplate.

FluidTemplate.Factory.RegisterTag<QuoteTag>("quote");

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024 1

I am adding it to the documentation

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024 1

using Fluid.MvcViewEngine; and not using FluidViewTemplate

from fluid.

tylersticka avatar tylersticka commented on August 15, 2024

@sebastienros Thank you for the suggestion!

Would the above go in Startup.cs, or somewhere else?

I tried this:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        TemplateContext.GlobalMemberAccessStrategy.Register<Person>();
        TemplateContext.GlobalFilters.AddFilter("slug", SlugifyFilter.Slugify);
        FluidTemplate.Factory.RegisterTag<SvgTag>("svg");
    }
}

But I still see a syntax error in the view:

Exception: Syntax error, expected: if, endif, elsif, else, unless, endunless, case, endcase, when, for, endfor, continue, break, comment, endcomment, raw, endraw, cycle, assign, capture, endcapture, increment, decrement, include, layout, renderbody, section, endsection, rendersection at line:0, col:3

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024

Try in the static constructor of the Startup class, to be sure it's run before anything.

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024

If that still doesn't work I assume I will need to change the way we register the view engine to provide a custom template type, which will be one of yours like in the documentation.

from fluid.

tylersticka avatar tylersticka commented on August 15, 2024

Just tried this:

public class Startup
{
    static Startup()
    {
        FluidTemplate.Factory.RegisterTag<SvgTag>("svg");
    }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        TemplateContext.GlobalMemberAccessStrategy.Register<Person>();
        TemplateContext.GlobalFilters.AddFilter("slug", SlugifyFilter.Slugify);
        // FluidTemplate.Factory.RegisterTag<SvgTag>("svg");
    }
}

Same result (no compilation errors, but same error in the view).

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024

Got it working, you actually need to change FluidViewTemplate:

        static Startup()
        {
            FluidViewTemplate.Factory.RegisterTag<MyTag>("mytag");
        }

from fluid.

tylersticka avatar tylersticka commented on August 15, 2024

@sebastienros I feel like I still might be missing something. When I change FluidTemplate to FluidViewTemplate, I see compilation errors like this:

Startup.cs(7,7): error CS0246: The type or namespace name 'FluidViewTemplate' could not be found (are you missing a using directive or an assembly reference?)

Here is a larger look at my Startup.cs file, so you can see what directives I'm using as well:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ClientName.Models;
using Fluid;
using FluidViewTemplate;
using FluidMvcViewEngine;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ClientName.Liquid.Filters;
using ClientName.Liquid.Tags;

namespace ClientName
{
    public class Startup
    {
        static Startup()
        {
            FluidViewTemplate.Factory.RegisterTag<SvgTag>("svg");
        }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            TemplateContext.GlobalMemberAccessStrategy.Register<Person>();
            TemplateContext.GlobalFilters.AddFilter("slug", SlugifyFilter.Slugify);
            // FluidTemplate.Factory.RegisterTag<SvgTag>("svg");
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().AddFluid().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024

Maybe you are missing a using statement. Look at the sample that does that in the repo.

from fluid.

sebastienros avatar sebastienros commented on August 15, 2024

Apparently you typed using statement by hand, hoping you'd be lucky ... you were not. Try to use Intellisense for that ;)

from fluid.

tylersticka avatar tylersticka commented on August 15, 2024

Apparently you typed using statement by hand, hoping you'd be lucky ... you were not. Try to use Intellisense for that ;)

Ha, in this case I wasn't the one who created the Startup.cs! But you're right, using Fluid.MvcViewEngine appears to have been missed, and restoring it fixes the problem.

Thanks for your help!

from fluid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.