JavaScript Data Inside post_content

Purpose:

Get access to JavaScript variables such as tags or section name for use in JavaScript snippets, such as ad targeting.

Steps:

  1. Create a new file inside mustache/roar/post/ folder. Let's call it post_scripts.html.
  2. Include it in your dynamic_content/postpage.xml template.
  3. <post_content template="roar/post/post_scripts" filters="section" />
  4. Add the <script> to your newly created file. Example:
  • <script>
        // get author name. If article has no author, default to 'unknown'.
        var author = "{{#original_user_subsite}}{{original_user_subsite.title}}{{/original_user_subsite}}{{^original_user_subsite}}unknown{{/original_user_subsite}}";
    
        // initialize array of post tags
        var tags = [];
    
        // initialize array of sections.
        var sections = [];
    
        // for each tag, push the tag name into our 'tags' array.
        {{#formatted_tags}}
        tags.push("{{name}}");
        {{/formatted_tags}}
    
        // for each section, push the section name into our 'section' array.
        // Note: this will usually only populate the primary section of the article, so array.length will be 1.
        {{#section}}
        sections.push("{{section.title}}")
        {{/section}}
    
        // Check results in browser dev tools.
        console.log(author, tags, sections);
    </script>

The {mustache} variables used can be found in your browser's console in dev tools under bootstrap object:

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
reply to topic
Like1 Follow
  • 1 Likes
  • 2 yrs agoLast active
  • 196Views
  • 1 Following