APratik
APratik

Reputation: 27

JS in .tpl file does not get compiled

I have the following code inside my .tpl file (I am using the Smarty template engine for my project).

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
   <script>
          $(document).ready(function(){
    $('.check:button').toggle(function(){
        $('input:checkbox').attr('checked','checked');
        $(this).val('uncheck all');
    },function(){
        $('input:checkbox').removeAttr('checked');
        $(this).val('check all');        
    })
})
</script>

<input type="button" class="check" value="check all" />

   <input type="checkbox" class="cb-element" /> Checkbox  1
   <input type="checkbox" class="cb-element" /> Checkbox  2
   <input type="checkbox" class="cb-element" /> Checkbox  3

However, in the final compiled php code, the JS function doesn't appear, as a result of which the toggle ("check all") button I want to create doesn't work. The final compiled php code, as seen in the browser looks like this :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
   <script>
          $(document).ready(function(),function())
})
</script>

<input type="button" class="check" value="check all" />

   <input type="checkbox" class="cb-element" /> Checkbox  1
   <input type="checkbox" class="cb-element" /> Checkbox  2
   <input type="checkbox" class="cb-element" /> Checkbox  3

I am new to using both jQuery and Smarty. Any help would be greatly appreciated. The jQuery function has been discussed in this SO post.

EDIT : I have put the JS inside {literal} and {/literal} and I can now see the JS function in the finally compiled php code. However, the JS function still does not respond! Why might that be?

Upvotes: 0

Views: 2234

Answers (2)

mtindall89
mtindall89

Reputation: 419

with smarty, and JS you add needs to be wrapped with {literal} and {/literal} so that smarty knows not to interpret the "{}" tags in the javascript as normal rather than as smarty normally interprets those tags

Upvotes: 2

Cysioland
Cysioland

Reputation: 1126

Consider putting your JS in separate file and linking to it. Smarty uses { and } as special characters.

Upvotes: 1

Related Questions