GithubHelp home page GithubHelp logo

Comments (17)

toddams avatar toddams commented on July 28, 2024 1

Yep, inheritance is supported.

Create a class:

public class BasePage<T> : TemplatePage<T>
	{
		public override Task ExecuteAsync()
		{
			return Task.FromResult(0);
		}
	}

In your view:

@inherits YourApplicationNamespace.BasePage<dynamic>

Replace dynamic with your model type, or leave it as it is.

About assemblies:
It takes dependencies from entryAssembly, so make sure that you have everything there.

from razorlight.

toddams avatar toddams commented on July 28, 2024 1

Part with injector is not correct. Is was just an example of how I use PreRenderCallback to fill in dependencies in templates.

Here is more complete example:

var configuration = EngineConfiguration.Default;

configuration.PreRenderCallbacks.Add(templatePage =>
{
	var page = (YourBaseType)templatePage;

	page.Html = /* Assign your property here */,
	page.MyOtherProperty = "Same here";
});

var engine = EngineFactory.CreatePhysical("C:\\", configuration);

/* Use engine then */

from razorlight.

toddams avatar toddams commented on July 28, 2024

HTML Helpers are not supported by RazorLight at the moment. @using, however, should work for specifying a namespaces

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

I have my own type FFHtmlHelper which I want to expose as a property Html on the views base type. Is there a way to set the base type?

Does it just use whatever assemblies are in the current appdomain, or do I need to specifically inject assemblies?

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

Is there a way to set some properties on the MyBasePage before executing the template?

from razorlight.

toddams avatar toddams commented on July 28, 2024

Yep, here is an example of code I use at RazorLight.MVC to inject properties values before rendering a page:

private static void AddEngineRenderCallbacks(IRazorLightEngine engine, IServiceProvider services)
{
	var injector = services.GetRequiredService<PropertyInjector>();
	engine.Configuration.PreRenderCallbacks.Add(template => injector.Inject(template));
}

Add your custom PreRenderCallback to EngineConfiguration

from razorlight.

toddams avatar toddams commented on July 28, 2024

Is everything clear @mcintyre321?

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

Not really, but I haven't had a chance to try it out yet!

I'm imagining I'm going to have to do something along the lines of:

class MyPageTempalteType<T> :  : TemplatePage<T>
{
    public HtmlHelper Html {get;set;}
    public override Task ExecuteAsync() => Task.FromResult(0);
}
...

var engine = EngineFactory.CreateEmbedded(typeof(MyPageTemplateType)) 



var injector = engine.services.GetRequiredService<PropertyInjector>();
engine.Configuration.PreRenderCallbacks.Add(template =>  {
    ((MyPageTemplateType) template).Html = GetHtmlHelper();
});

...

var model = new SomePageModel()
{
    Title = "Hello, world",
    Description = "Some text here"
};
string result = engine.Parse("SomePage", model); 

Is that about right?

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

Is it performant to new up a new engine each time, or should it be cached? I may have different values for MyOtherProperty etc.

from razorlight.

toddams avatar toddams commented on July 28, 2024

Yea, that might be a problem. First solution that comes to my mind - is to add "Remove" method to PreRenderActionList, so you can replace a callback before each render

from razorlight.

toddams avatar toddams commented on July 28, 2024

Special for you, I added an additional overload to "Parse" method, which accepts Action<TemplatePage>, so you can add callbacks for each page individually, and not to rely on global PreRenderCallbacks of EngineConfiguration.

Here is a full example:

Code:

string result = engine.Parse("file.cshtml", model: 1337, viewBag: null, prerenderCallback: new Action<TemplatePage>((t) =>
{
	var page = (BasePage<int>)t;
	page.Property = 1337;
}));

View:

@inherits RazorLight.Sandbox.BasePage<int>

<html>
    @Property
</html>

BasePage.cs

public class BasePage<T> : TemplatePage<T>
{
    public int Property { get; set; }
    public override Task ExecuteAsync()
    {
        return Task.FromResult(1);
    }
}

Rendered result:

<html>
    1337
</html>

from razorlight.

toddams avatar toddams commented on July 28, 2024

Let me know if it works for you, so I can finally close this issue :)

from razorlight.

joetherod avatar joetherod commented on July 28, 2024

Regrading namespaces, I see there is a collection for namespaces. Can I just add the namespaces there? If so, how? I added them to that collection and it still didnt work. The razor I have uses the namespaces in the web.config, so they are not listed in the actual html.

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

Thanks for the new feature! I'm still trying to get this to work, have discovered NUnit doesn't work in netcore1.1 which is slowing me down a little...

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

Still trying to get a custom base type to work...
I tried setting the using a custom PageFactory (in a custom EngineFactory copied from the Embedded one):

      IPageFactoryProvider pageFactory = new DefaultPageFactory((key) =>
            {
                ITemplateSource source = manager.Resolve(key);

                string razorTemplate = core.GenerateRazorTemplate(source, new ModelTypeInfo(typeof(FormFactoryTemplateBase)));
                var context = new CompilationContext(razorTemplate, configuration.Namespaces);

                CompilationResult compilationResult = configuration.CompilerService.Compile(context);

                return compilationResult;
            });

but the template always compiles with TemplatePage<TModel> not FormFactoryTemplateBase* - is it because [BaseType] is a const, and isn't being set from my ModelTypeInfo?

*which inherits TemplatePage

from razorlight.

toddams avatar toddams commented on July 28, 2024

Example that I provided works, I tested it. You cast TemplatePage to your template base type and it works. You don't need custom PageFactory

from razorlight.

mcintyre321 avatar mcintyre321 commented on July 28, 2024

I see, I had to use an explicit @inherits statement (rather than setting the base type in the factory). It seems to be working now! thanks!

from razorlight.

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.