Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Assign a (string?) value to a variable in a PHP template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Sat Jan 14, 2017 9:24 am    Post subject: Assign a (string?) value to a variable in a PHP template Reply with quote

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

Code:
<p></p>

I have tried different ways, but all unsuccessful.

How can I do this?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jan 14, 2017 7:14 pm    Post subject: Re: Assign a (string?) value to a variable in a PHP template Reply with quote

Code:
<p>{$citation->getRawCitation()|strip_unsafe_html}</p>

Parenthesis forces re-evaluation of an expression on PHP level.
I.e.
Code:
<?php

$x = ($y);

will first evaluate $y, then assign evalueated value to $x.

Next time, please set
error_reporting = -1
display_errors = On
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Jan 15, 2017 12:34 am    Post subject: Reply with quote

I would try:
Code:
{assign "citat" $citation->getRawCitation()|strip_unsafe_html}

Then $citat should be available afterwards.
Back to top
View user's profile Send private message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Sun Jan 15, 2017 8:16 am    Post subject: Reply with quote

Thank you for help and pointing out using error display. I have checked both answers, unfortunately none worked Sad

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);
  ?&gt;
 <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

Code:
 <p></p>


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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Jan 15, 2017 3:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Jan 15, 2017 4:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Mon Jan 16, 2017 11:02 am    Post subject: Reply with quote

Great, it works. Thank you.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jan 17, 2017 1:44 pm    Post subject: Reply with quote

I don't understand, why you want to use {assign}.
You're just making your templates less readable.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP