How to use smarty {section} within {literal} JavaScript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayatrib
    New Member
    • Mar 2010
    • 4

    How to use smarty {section} within {literal} JavaScript?

    I want to populate a dropdown menu like this:

    event.php

    Code:
    $sql="select * from tbl_clientdetails";
    $rs=$conn->Execute($sql);
    $client=$rs->getrows();
    STemplate::assign('client',$client[0]);
    event.tpl

    Code:
    {literal}
    	<script language="javascript">
    	function searchclient(searchvalue)
    	{
    		if (searchvalue == 'clients') {	
                      var clients = "<select name='searchfile' class='input'>";	
    		{section name=a loop=2}
    	           clients=clients+"<option value='{$client[a].name}'>{$client[a].name}</option>";
    		{/section}
    		 clients=clients+"</select>";		 
    		 document.getElementById("searchfile").innerHTML = clients;
    		 } 
    		else {
    		 var clients = "<input name='searchfile' type='text' />";
    		 document.getElementById("searchfile").innerHTML = clients;
    		 }
    	 }
    	</script>
    	{/literal}

    in boby section of event.tpl
    Code:
    <form method="post" action="eventsearch.php" name="frm2" >
    <div align="right">
                      <span id="searchfile">
    </span>
    
    <select class="input" onChange="searchclient(this.value)" name="searchcriteria">
    <option selected="" value="enclosure">Enclosure</option>
    <option value="clients">Client</option>
    </select> 
    
    <input class="button" type="image" src="../images/search1.jpg" name="submit" />
    </div>                  
    <div align="right"></div>
    </form>
    Last edited by Atli; Mar 16 '10, 01:41 PM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Everything within {literal} blocks is displayed as-is, including other smarty syntax. (That's kind of the point.)

    So to use {section} in your JavaScript, you need to break out of the {literal} block before using it. Keep in mind that the {literal} around the JavaScript block is only there because of the curly-brackets used in the JavaScript syntax. It doesn't really need to cover the entire code, just the parts that would mess up the Smarty rendering.

    You could also check out {ldelim},{rdeli m}. They may even better suited for this situation than {literal}, come to think about it.

    Comment

    Working...