-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store templates as string #239
Comments
react-templates are already compiled, they don't live anywhere as strings during runtime. They are functions. You should address your problem differently, e.g. treat your 'tpl1' and 'tpl2' as components and render then via |
if you want to "compile" your strings at runtime you could try to call |
@dthpth what would Would something like this work?
|
@dthpth at runtime you can't use it because react-templates adds no runtime library, e.g. @LeonardoGentile why not just that:
still, I think there are better ways to handle the problem. |
@nippur72 so it is technically possible (but not advisable) to implement the solution I posted above? You are right, there are far better ways to solve this but unfortunately this is part of a requirement. The object
contains potentially hundreds of templates and is stored in a single file. In particular that file is standard es5 that should not go through any compilation, just a plain js file exporting a global var. Then the react app will get that global object containing all the templates and use/compile the correct ones depending on certain conditions. I know it is a poor/ugly design but it is required to be implemented this way 🙄 |
yes it's technically possible, I have done something similar in the past, and certainly the Playground does it already. The two main drawbacks that I see are:
Something like this: var source = /* your template */ ;
var reactTemplates = require('react-templates/src/reactTemplates');
var options = { /* rt command line options */};
var sfunction = reactTemplates.convertTemplateToReact(source, options);
var code = eval(sfunction); |
Thanks for your help!
Are you planning to improve client-side support or probably it is better to look for alternative solutions? Probably something like this https://github.com/TroyAlford/react-jsx-parser ? |
I'm not a maintainer but I don't think it will be improved in the foreseeable future. If you are not tied to React, you might want to look into Riot.js which has template compilation at runtime. Vue is also another alternative. |
Thanks, unfortunately I'm tied to react |
Hello,
I'm having this requirements: in a single js file I need to store multiple rt templates as strings.
For example:
Then import the
templates
object and select one of its attribute. Finally passing it to the component that should render it.So what I need is a sort of a compilation process of the template, similar of what was available in (ahem) angular1
$compile(element)(scope)
Is this possible?
The text was updated successfully, but these errors were encountered: