View previous topic :: View next topic |
Author |
Message |
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Sat Jan 14, 2017 9:24 am Post subject: Assign a (string?) value to a variable in a PHP template |
|
|
I have following code in the template, that works perfectly OK:
Code: | <p>{$citation->getRawCitation()|strip_unsafe_html}</p> |
Now I would like to assign the value to a variable, modify it and output it. The first attempt was (without any modification, just get the value to a variable and output it):
Code: | {$citat=($citation->getRawCitation()|strip_unsafe_html)}
<p>{$citat}</p> |
But the result in the output HTML is
I have tried different ways, but all unsuccessful.
How can I do this? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Jan 14, 2017 7:14 pm Post subject: Re: Assign a (string?) value to a variable in a PHP template |
|
|
Code: | <p>{$citation->getRawCitation()|strip_unsafe_html}</p> |
Parenthesis forces re-evaluation of an expression on PHP level.
I.e.
will first evaluate $y, then assign evalueated value to $x.
Next time, please set
error_reporting = -1
display_errors = On |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Jan 15, 2017 12:34 am Post subject: |
|
|
I would try:
Code: | {assign "citat" $citation->getRawCitation()|strip_unsafe_html} |
Then $citat should be available afterwards. |
|
Back to top |
|
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Sun Jan 15, 2017 8:16 am Post subject: |
|
|
Thank you for help and pointing out using error display. I have checked both answers, unfortunately none worked
1. Using <?php
This is the code
Code: | <?php
$citat = ($citation->getRawCitation()|strip_unsafe_html);
?>
<p>{$citat}</p> |
The output on the web page:
Code: | getRawCitation()|strip_unsafe_html); ?> |
And the HTML results:
Code: | <!--?php
$citat = ($citation--->getRawCitation()|strip_unsafe_html);
?>
<p></p> |
I have two questions regarding this apporach:
- It looks to me that the directive <?php is not interpreted correctly as the output is ""
- is it possible that a variable defined inside the directive <?php is available outside of the directive, that is later on
I haven't found any useful error.
2. Using {}
This is the code
Code: | {assign "citat" $citation->getRawCitation()|strip_unsafe_html}
<p>{$citat}</p> |
There was nothing on the web page.
This is the HTML output
The errors:
Code: | syntax error: invalid attribute name: '"citat"'
syntax error: invalid attribute name: '$citation->getRawCitation()|strip_unsafe_html'
syntax error: assign: missing 'var' parameter |
Any useful information how to proceed? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Jan 15, 2017 3:24 pm Post subject: |
|
|
Please let us know how you have instantiated Smarty, and whether you have a template we are trying to code and have Smarty render.
This: Code: |
<?php
$citat = ($citation->getRawCitation()|strip_unsafe_html);
?>
<p>{$citat}</p> | suggests you have one PHP script that also contains some HTML in it. Nothing wrong with that except Smarty is not involved.
As a PHP statement:
$citat = ($citation->getRawCitation()|strip_unsafe_html);
the bold part is not valid PHP. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Jan 15, 2017 4:15 pm Post subject: |
|
|
Example 7.10 at:
http://www.smarty.net/docs/en/language.function.assign.tpl
implies that this can be done.
But maybe the shorthand notation requires literal strings while the longhand notation does not.
Code: | {assign var="citat" value=$citation->getRawCitation()|strip_unsafe_html}
<p>{$citat}</p> |
|
|
Back to top |
|
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Mon Jan 16, 2017 11:02 am Post subject: |
|
|
Great, it works. Thank you. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Jan 17, 2017 1:44 pm Post subject: |
|
|
I don't understand, why you want to use {assign}.
You're just making your templates less readable. |
|
Back to top |
|
|