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

{include ...} and inline Javascript

 
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
chitoiur
Smarty n00b


Joined: 18 Dec 2011
Posts: 3

PostPosted: Sun Dec 18, 2011 9:49 pm    Post subject: {include ...} and inline Javascript Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Dec 18, 2011 10:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Sun Dec 18, 2011 10:32 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
chitoiur
Smarty n00b


Joined: 18 Dec 2011
Posts: 3

PostPosted: Sun Dec 18, 2011 10:40 pm    Post subject: Reply with quote

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


Joined: 05 Jul 2011
Posts: 4

PostPosted: Sat Jul 13, 2013 10:13 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
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