Chameleon templates for javascript files?

后端 未结 1 717
说谎
说谎 2020-12-18 07:39

I am developing a simple pyramid application where I am using JQuery to do AJAX requests. I have until now had my javascript code within my chameleon templates. Now I want t

1条回答
  •  佛祖请我去吃肉
    2020-12-18 08:22

    Yes; you generally put context-specific information like expanded routes into the templates and access this information from your (static) JavaScript libraries.

    Including the context info can be done in various ways, depending on taste:

    1. You could use a data attribute on a tag in your generated HTML:

      
          ...
      
      

      which you then, in your static JS code load with the jQuery .data() function:

      var viewurl = $('body').data('viewurl');
      
    2. Use a made-up LINK tag relationship to include links in the document head:

      
          
          ...
      
      

      which can be retrieved using $('link#viewurl').attr('href'), or $('link[rel=ajax-datasource]').attr('href'). This only really works for URL information.

    3. Generate JS variables straight in your template, to be referenced from your static code:

      
          ...
          
      
      

      and these variables are referable directly with contextVariables.viewurl.

    0 讨论(0)
提交回复
热议问题