Skip to content
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

Add ability to render view from string #19

Closed
RomeroMsk opened this issue Feb 16, 2015 · 15 comments
Closed

Add ability to render view from string #19

RomeroMsk opened this issue Feb 16, 2015 · 15 comments
Labels
type:enhancement Enhancement

Comments

@RomeroMsk
Copy link
Contributor

I can't find the way to render a view from string (not from file). I want to use template engine (Twig or Smarty) to render texts from templates stored in DB. For example, store email message template in DB table and generate email body from it on the fly.

@klimov-paul
Copy link
Member

Use old good eval().

@nineinchnick
Copy link
Contributor

@klimov-paul why would you ever recommend eval()? @RomeroMsk you're asking what to use instead of render() in a controllers action? You can use renderContent.

@RomeroMsk
Copy link
Contributor Author

@klimov-paul, are you serious?
@nineinchnick, yes I need to use render() method (or something like renderString()) with string content instead of view file path.

@nineinchnick
Copy link
Contributor

I've updated my response above, use renderContent.

@klimov-paul
Copy link
Member

I've updated my response above, use renderContent.

I can not see how renderContent() can help here. It will just pass the PHP code as a plain string into the layout file.

As far as I understand you wish to evaludate some string, stored in DB, as a view file content. There is no way for it instead of using eval().
As far as I remember Smarty has built in eval function (or plugin), which allows evalution of a string as Smarty template content. As for Twig - I am unsure. For plain PHP view - native eval() is only option.

@RomeroMsk
Copy link
Contributor Author

@nineinchnick, can you show an example of how to use this with Twig and not in controller context? Let's say we have $template = 'Hello, {{ username }}!' and want to put render result into $result, using ['username' => 'Test'] as parameters.

@RomeroMsk
Copy link
Contributor Author

@klimov-paul, I've created this issue to ask for an ability to use stirng content instead of files in ViewRenderer. I don't like your workaround.

@klimov-paul
Copy link
Member

klimov-paul commented Feb 16, 2015

This is not walk around - this is the only solution. Of course we can instoduce another method in the View class, but it will do just the same thing.
For smarty you can use following:
http://www.smarty.net/docs/en/language.function.eval.tpl
For Twig - I don't know.

I suppose for your case a special tool should be used like following:
https://mustache.github.io/

@nineinchnick
Copy link
Contributor

@RomeroMsk I think this does not concern Yii at all, you're asking how to process a Twig template. See twig docs.

@RomeroMsk
Copy link
Contributor Author

@klimov-paul, yes, Mustache is good for this, but it's logic-less. Thanks, anyway.
@nineinchnick, I think you're right, thank you.

@lynicidn
Copy link
Contributor

@baijunzhen
Copy link

@RomeroMsk 你可以这样
Yii::t('yii','you email body {username},send at{date}',['username'=>'foo',‘date’=>date('Y-m-d')]);

Deele referenced this issue in Deele/yii2 Nov 7, 2017
Stores string in temporary file, renders and returns result using renderPhpFile

Related to #6252 #7309
@samdark samdark reopened this Apr 24, 2019
@samdark samdark transferred this issue from yiisoft/yii2 Apr 24, 2019
@samdark
Copy link
Member

samdark commented Apr 24, 2019

yiisoft/yii2#15116

@mikk150
Copy link
Contributor

mikk150 commented Apr 24, 2019

I have something that renders strings stored in SQL
https://github.com/mikk150/yii2-sql-renderer

adopt it a bit and do your magic

edit:// I use it with conjuction with twig strings, because I do not trust others that much

@samdark samdark added the type:enhancement Enhancement label Sep 12, 2019
@samdark
Copy link
Member

samdark commented May 13, 2021

After thinking, I don't think we should explicitly support that because it will suggest doing that is alright, especially with eval(). #122 may be an option.

@samdark samdark closed this as completed May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

7 participants