|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
chitoiur Smarty n00b
Joined: 18 Dec 2011 Posts: 3
|
Posted: Sun Dec 18, 2011 9:49 pm Post subject: {include ...} and inline Javascript |
|
|
Hi, I am trying to do the following:
var html = '{include file='template.tpl}';
$(html).appendTo('#some_id');
I can't get it to work 100% properly. The reason is that when inlined like that, the resulting string will be multi-line. The way I fixed it the first time around was just to add a backslash to the end of each line in the template. That works fine, but the template is unusable if I want to include it normally. Then I tried using {strip}, which works just fine, except it doesn't fully manage to get rid of the last endline (which I think is a bug), as the Javascript complains about multiline strings and the lack of backspace.
Has anyone else come across this and found a good solution?
Thanks,
Robert |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Dec 18, 2011 10:08 pm Post subject: |
|
|
Where did you put the {strip} tags? In 'template.tpl'? If yes make sure that you have no linebreak after the {/strip} at template end. |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Sun Dec 18, 2011 10:32 pm Post subject: |
|
|
Code: | {capture name="bananas"}{strip}{include file="foo.bar"}{/strip}{/capture}
var html = '{$smarty.capture.bananas|escape:"javascript"}'; | is the way to go here… _________________ Twitter |
|
Back to top |
|
chitoiur Smarty n00b
Joined: 18 Dec 2011 Posts: 3
|
Posted: Sun Dec 18, 2011 10:40 pm Post subject: |
|
|
rodneyrehm wrote: | Code: | {capture name="bananas"}{strip}{include file="foo.bar"}{/strip}{/capture}
var html = '{$smarty.capture.bananas|escape:"javascript"}'; | is the way to go here… |
Thanks! I'm almost there. How do I load a template without actually pulling it into the page? All I want is for "bananas" to be populated.
Edit: scratch that, I got it! Thanks again! |
|
Back to top |
|
krodelabestiole Smarty n00b
Joined: 05 Jul 2011 Posts: 4
|
Posted: Sat Jul 13, 2013 10:13 pm Post subject: |
|
|
here is a method to keep spaces and line breaks, just adding \ before each one, which works with javascript :
Code: | {capture name="html"}
{include file='template.tpl'}
{/capture}
'{$smarty.capture.html|replace:"\n":"\\\n"}' |
|
|
Back to top |
|
|
|
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
|