-
Notifications
You must be signed in to change notification settings - Fork 24
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
Question about template variables #4
Comments
Your question is not improper, and thanks again for your interest. Reason are:
If you are intersted in a more in depth comparison with Plates features I did a blog post. |
I see. Thanks for your explanation. While I agree with your points, I have some concerns about the 'On Plates data access' section. Having worked countless hours with native php templates, I made the following observations:
About autoescaping - I guess there is no difference for the end user between escaping a variable on demand, when accessing it from the template with $this->my_var, and escaping all of the variables that are passed to the render function, thus providing escaped scoped vars to the template. Do you see room for an option like 'use_scoped_variables', that would allow to access the template variables without 'this' ? |
I've worked countless hours with templates engines, not native php one, but in this regard there is no difference between native or compiled, and I encountered a lot of occasions where variables may be there or not, expecially when data comes from external sources, like API, file parsing... Moreover, if you know that undefined variables equals empty string, you can use undefined variable by design. Let's assume that by design a page may has or may not a subtitle. Page template in Plates will probably contain:
or a more verbose An alternative would be always assign subtile var to page, but in that case the control structure is moved from template code to data generation code (controller or whatever), that is sure better, but no need for it is even better IMO.
I'm pretty agree that 'strict_variables' would by a nice addition I'm going to add myself an issue for it.
Plates documentation itself encourages people to do If we agree that unescaped output is bad (and I agree) Foil gives an easier access to escaped data while unescaped data must be accessed intentionally with
Yes, I do. Generally speacking I write open source code for things that I like to have but I can't find. This means the default behavior of my code reflects my preferences. But I don't see reason why I have to force people on my preferences. |
Thanks for the detailed explanation. My experience with templates is 95% connected with html markup, that's why in my cases $subtitle would be wrapped by span (div, paragraph etc.) and I would use conditional structure to omit this markup regardless of how I access $subtitle. I see this is not valid for the cases you mention (API, file parsing), so I am biased here. Another attempt to explain my view about autoescaping. $engine->render('a-template', $template_variables_array); Why can't the template engine just escape and then extract $template_variables_array before including the template file, so all of the $template_variables_array items would appear as escaped scoped variables in the template ? The original unescaped $template_variables_array can be kept in order to be able to access the raw variables with $this->raw('my_var');. Does this makes sense or I miss the point ? I didn't mean to question your purposes for creating Foil, just discussing my views on the topic. I will think about a PR. |
This is the biggest issue with autoescape in native PHP templates. Variables passed to templates are not always strings. Strings can be easily escaped. Array can as well, by recursively parse them. Problem are objects. While you can get object properties using Example: class Post {
private $data;
public function __construct(array $data) {
$this->data = $data;
}
public function __get($var) {
return isset($this->data[$var]) ? $this->data[$var] : '';
}
}
$post = new Post(array('foo' => 'Bar'));
echo $post['foo']; // echo 'Bar' If the This problem does not exists for compiled template engines, because they first echo the value (in a sandboxed environment), then escape the echoed value. The point of If the |
Hi again!
I'm curious about the need to use $this->my_var while accessing data in templates. Is there any particular reason (performance, escaping etc.) for not using locally scoped variables like $my_var ? Plates does allow it AFAIK.
I didn't dig into Foil that much, so my question could sound improper.
Regards,
Stoyan
The text was updated successfully, but these errors were encountered: