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

Q: Passing vars into popup windows. Best practice, any1?

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


Joined: 27 May 2003
Posts: 3

PostPosted: Tue May 27, 2003 2:38 am    Post subject: Q: Passing vars into popup windows. Best practice, any1? Reply with quote

Hello everyone,

I was wondering if you would give me an advice on how pass string variables into popup windows.

Code:

...
{capture name=submitted_data}
<table width="500" border="0" cellspacing="0" cellpadding="0">
{section name=rows step=1 loop=$submitted_keys show=$submitted_keys}
    <tr valign="top">
    <td class="{cycle ...}" nowrap><div class="{cycle ...}">{$submitted_keys[rows]|escape:"htmlall"}</div></td>
    <td class="{cycle ...}" nowrap><div class="{cycle ...}">{$submitted_values[rows]|escape:"htmlall"|wordwrap:40:"\n":true}</div></td>
    </tr>
{/section}
</table>
{/capture}
{$smarty.capture.submitted_data}
...
Click <a href="printpop.php" onClick="window.open('printpop.php', 'prnWin', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=no, width=500, height=400'); return false">here</a>.


As you can see, within this tpl i capture an entire section (which potentially may become a pretty long string) into a $smarty.capture.submitted_data. Then i display what's in this variable immediately after {/capture}, and this technique gives me no problem whatsoever.

Now, i need to open a small window and display the content of that variable again. What would be the best way to do that? In other words, how do i keep the content of this variable if i need to open a new window? Again, it may potentially have a pretty long string assigned to it.

This is what i've been battling with so far:

This gives me a javascript error "Unterminated string constant", no matter what i do:

Code:

<img src="p.gif" onClick="show_win('{$smarty.capture.submitted_data|escape}'); return false;">


If i assign a much simpler string (no html) to $smarty.capture.submitted_data, javascript error disappears.

I, of course, am open to any suggestion, and i'm flexible as far as abandoning this little {capture} technique of mine as well...as long as a method that is suggested to me seems more elegant.

Thanks in advance,

ALX
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue May 27, 2003 8:08 am    Post subject: Reply with quote

escape your captured data to suffice javascript-quoting:

Code:

<img src="p.gif" onClick="show_win('{$smarty.capture.submitted_data|escape:javascript}'); return false;">
Back to top
View user's profile Send private message Send e-mail Visit poster's website
collidr
Smarty n00b


Joined: 27 May 2003
Posts: 3

PostPosted: Tue May 27, 2003 10:08 pm    Post subject: Reply with quote

Thanks a lot, messju, for responding.

Quote:
escape your captured data to suffice javascript-quoting


When you suggested the javascript type of an escape, it felt redundant to me at first, because i knew i'd tried that. But what i didn't try is to have all double-quotes removed from my captured output. So I went back and stripped the code off the double qutation marks:

Code:

{capture name=submitted_data}
<table width=500 cellspacing=0 cellpadding=0>
{section name=rows step=1 loop=$submitted_keys show=$submitted_keys}
<tr valign=top>
<td class={cycle ...} nowrap><div class={cycle ...}>{$submitted_keys[rows]|escape:htmlall}</div></td>
<td class={cycle ...} nowrap><div class={cycle ...}>{$submitted_values[rows]|escape:htmlall|wordwrap:40:'\n':true}</div></td></tr>
{/section}
</table>
{/capture}


Suddenly my onClick event stopped complaining...
And that's after i've spent a day trying to hack my way around this javascript thing. I was so desperate I even tried adding a

Code:

case 'xeh':
   // escape every character from hex
   $return = '';
   $string = explode('%', $string);
   array_shift($string);
   foreach ($string as $hexstr) {
   $return .= chr(base_convert($hexstr, 16, 10));
   }
return $return;


section into my <b>modifier.escape.php</b> plugin so I could hex-encode my caputred stuff, then pass it using GET into a php file and so on... Smile That didn't work because my string would get truncated at 2038 character.

To sum things up, yeah, i ended up generating a pure html output
using a series of document.write() javascript methods and something still tells me it's not the best way, but at least it works for now.

Thanks again for your reply!

ALX
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue May 27, 2003 10:52 pm    Post subject: Reply with quote

This isn't a direct answer to your problem, but I agree that the solution looks more complicated than the problem you are trying to solve Wink

Personally, I'm against doing too much intermixing of languages in the templates. I find that it is somewhat confusing and that it often leads to syntatical feats best left to heroes.

Instead, I break my templates up, roughly by target language. That means that dynamically generated Javascripts have their own files which can be fetched or {included}. That means you can either rely on the cache or you can serialize the data to a string or you can use sessions to span multiple pages. Plugins can then encapsulate the dirty work of orchestrating the retrievals (and possibly storage). ASIDE: In my mind, one of Smarty's most appealing features is its API -- it is truly an extensible framework!

It also means your snippets are more readily reused elsewhere, which also implies that portions that are common over many pages need only be compiled once (and perhaps served from the cache). Further, all of your resulting templates are more focused, making it easier for the developers. IMHO, the entire processing chain becomes more graspable. Lastly, separating details like this usually means you can put in a little bit more scaffolding and automation controls around your application. I am motivated by structural and organizational issues, so perhaps I'm being too zealous in this case Wink

Just an idea.

Also, thank-you for posting! Your original message and your follow-up were both interesting and well laid-out. I hope to see you around!

EDIT:

An additional benefit is that you can use a php wrapper to allow you to directly call your templated javascript files from your HTML pages (to leverage your client's browser cache). This is also true for templated CSS files.

EG:

Assume JS.PHP which ultimately does at minimum: $smarty->display( $_REQUEST( 'src' ) );
Code:
<SCRIPT language="JavaScript" src="JS.php?src=mycode.js&myoption=mysetting&etc" type="text/javascript"></SCRIPT>


Similarly, for CSS:
Code:
<LINK rel="stylesheet" HREF="CSS.php?src=mystyle.js&theme=mytheme&etc" />


It is often easier (for caching reasons) to use individual wrapper pages (ie. somecode.js.php exclusively for somecode.js.tpl) but I think it a little obtuse to do it that way.

(NB: yes, there is a limit to this: namely, where too many file retrievals result in the ensuing connection overhead outweighing the supposed cache benefits -- at least during the initial site load, which is often an important factor. test test test)


Last edited by boots on Wed May 28, 2003 12:05 am; edited 6 times in total
Back to top
View user's profile Send private message
collidr
Smarty n00b


Joined: 27 May 2003
Posts: 3

PostPosted: Tue May 27, 2003 11:26 pm    Post subject: Reply with quote

sessions! sessions! that's it!

a quick thank you, and brb with the success story, i'm sure Smile

_codelab is now in session_

ALX
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed May 28, 2003 7:11 am    Post subject: Reply with quote

collidr wrote:

When you suggested the javascript type of an escape, it felt redundant to me at first, because i knew i'd tried that. But what i didn't try is to have all double-quotes removed from my captured output. So I went back and stripped the code off the double qutation marks:

...

Suddenly my onClick event stopped complaining...
And that's after i've spent a day trying to hack my way around this javascript thing.


umm, my fault.
{$smarty.capture.submitted_data|escape:javascript} only escapes your string to be valid in javascript, but you have to escape it to html to be valid inside html (like onclick).

for example:

this is quoted correct for any $var:
Code:

<script language="javascript">
alert('{$var|escape:javascript}');
</script>


but in an event-handler (this is html-context in the first place, not javascript):
it would have to be:

Code:

<a href="..." onlick="alert('{$var|escape:javascript|escape:html}')">


it seems you are going another way now anyway, this is just for the records. Smile
Back to top
View user's profile Send private message Send e-mail 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