Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Insert Smarty variable in dynamic HTML

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
hansr
Smarty n00b


Joined: 22 Aug 2019
Posts: 3

PostPosted: Thu Aug 22, 2019 8:03 am    Post subject: Insert Smarty variable in dynamic HTML Reply with quote

Hello,

I want to insert a Smarty variable into a dynamic html string built in PHP. Something like:

Code:

$dynhtml='<b>Huisnummer: {$number} </b>';
$number="15";

$smarty->assign('dynhtml', $dynhtml);
$smarty->assign('number', $number);


The template:

Code:

{$dynhtml}


Unfortunately the variable $number is not parsed so the text "Huisnummer: {$number}" is printed.

Is it possible to construct dynamic html this way?

Thanks for your answers,

Regards,

Hans
Back to top
View user's profile Send private message
bsmither
Smarty Pro


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

PostPosted: Fri Aug 23, 2019 7:09 am    Post subject: Reply with quote

Try in this order:
Code:
$number="15";
$smarty->assign('number', $number);

$dynhtml=$smarty->fetch('string:<b>Huisnummer: {$number} </b>');
$smarty->assign('dynhtml', $dynhtml);
Back to top
View user's profile Send private message
hansr
Smarty n00b


Joined: 22 Aug 2019
Posts: 3

PostPosted: Fri Aug 23, 2019 9:37 am    Post subject: Reply with quote

Thanks very much!

That is exactly what I needed!

regards,

Hans.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Fri Aug 23, 2019 8:17 pm    Post subject: Reply with quote

Why don't you just qrite it in the template?
That looks like gross misunderstanding of the templating purpose.
Back to top
View user's profile Send private message
hansr
Smarty n00b


Joined: 22 Aug 2019
Posts: 3

PostPosted: Sat Aug 24, 2019 5:18 pm    Post subject: Reply with quote

Not really,

The website has a number of forms that are all build with standard templates.

This one form however is a completely dynamic subscription form for a great number of events with each a different set of inputs and options, governed by a database. Therefore a static template is not an option.

Because all the other forms are made with Smarty templates, I am trying to build this one with Smarty too so I can keep as close to an MVC solution as possible.

regards,

Hans.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Sat Aug 24, 2019 8:08 pm    Post subject: Reply with quote

Then write a form builder that does what you want, or use SmartySecurity module and store form template in the database, editable by user(s).
Resorting to "string:" template is a least performant route, IMO.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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