1

I just want to print a value from Craft into a JavaScript function to filter a dashboard.

The function is this:

showOnly(filterName, values);

I tried this:

showOnly('Short Names','{{ entry.title }}');

But that did not work. And yes, the filter does work If I hard wire the value that Craft/Twig should print. But, I need it to match the title of the page as one navigates from entry to entry.

UPDATE

Here is the actual showOnly function in its entirety:

function showOnly(filterName, values) {
    // Think a single select filter.
    sheet = mainViz.getWorkbook().getActiveSheet();
    if(sheet.getSheetType() === 'worksheet') {
        sheet.applyFilterAsync(filterName, values, 'REPLACE');
    } else {
        // for dashboards
        worksheetArray = sheet.getWorksheets();
        for(var i = 0; i < worksheetArray.length; i++) {
            worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE');
        }
    }
};
  • 1
    And what value should it print? String or array of strings (as values parameter name suggests)? Show us what it currently prints; – Krzysztof Boduch Nov 16 '16 at 22:11
  • I don't know what it is "printing" to the function. I can't see it. The dashboard filters to nothing. How do I add a console event to depict what is being printed (if anything). I added the actual function to the question above... Thank you – Hellyar Nov 17 '16 at 13:44
  • I think we need more info about the context here to help you - is this a front end dashboard, or in a back end plugin? What exactly is not working - can you inspect the source and see the twig replacing occurring (you should be able to). – Jeremy Daalder Nov 17 '16 at 23:35
2

That is the correct way of doing it but you need to be sure that entry is a variable and set to an entry model at that point. And that you're in an appropriate part of your template in general for outputting the code.

E.g. this works for analytics for example, placed at the end of my main structure block.

{# ANALYTICS GOOGLE #}
{% set js %}
<!-- Google Code for Website Conversions Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXXXXXXXX";
var google_conversion_value = {{order.totalPrice}};
var google_conversion_currency = "AUD";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXXXXX/?value={{order.totalPrice}}&amp;currency_code=AUD&amp;label=XXXXXXXXX&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

{% endset %}

{% includejs js %}
| improve this answer | |
  • Entry is defined. I have no template errors and the rest of the template works. This is the only thing that does not, and I assume its because I am inserting a Twig into a javascript function inside of a script tag. See update to original question above the full include statement. – Hellyar Nov 17 '16 at 13:49
  • See my comment above. In general it does work, so I need a bit more info to work out what you're doing and where it might be going wrong. – Jeremy Daalder Nov 19 '16 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.