• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.1 Latest
Buy Hosting

How do I insert associative arrays in plugin templates?

Scheduled Pinned Locked Moved NodeBB Development
11 Posts 4 Posters 2.7k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by
    #1

    Hi, I have a simple problem. In my plugin code I am trying to insert this variable into the javascript in one of my templates:

    var ticketTypes = {
        adult: 10.00,
       student: 7.50,
       child: 5.00
      }
    

    My best approach so far is to pass it to the template like this in a res.render call:

     {"tickettypes": JSON.stringify(ticketTypes)}
    

    (Without JSON.stringify, it doesn't show up in the results at all)

    It makes it into my page in a javascript section like this:

     var tickettypes = $.parseJSON('{tickettypes}'.replace(/{/g, "{").replace(/}/g, "}"));
    

    The entity replacement is necessary because the value makes it into the template as:

    '{"adult":10,"student":7.5,"child":5}'
    

    So it "works" but feels hacky. Is there a better way to get an associative array in the client side javascript?

    Thanks!

    psychobunnyP 1 Reply Last reply
    0
  • psychobunnyP Offline
    psychobunnyP Offline
    psychobunny
    replied to jongarrison on last edited by
    #2

    ajaxify.data.get('foo') ought to do it

    1 Reply Last reply
    1
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by
    #3

    @psychobunny said:

    ajaxify.data.get

    Woah! That's kick ass! There is a lot of data ready to go in that ajaxify.data object. That changes the way I think about loading data into NodeBB templates.

    For anyone else interested, in your browser js console just type "ajaxify.data" to see what NodeBB already has loaded for you.

    Is this the best spot to learn more about ajaxify? http://4nf.org/

    1 Reply Last reply
    1
  • psychobunnyP Offline
    psychobunnyP Offline
    psychobunny
    wrote on last edited by
    #4

    Ajaxify is something we developed in-house (along with templates.js, translator.js, amongst others). The link you posted is unrelated, unfortunately.

    That said, we could use with better documentation for our major components (damn! That's really lacking a lot of information.)

    If you ever have free time please contribute (the edit button is on the top right).

    In any case, I'm glad you figured it out 🙂

    1 Reply Last reply
    0
  • psychobunnyP Offline
    psychobunnyP Offline
    psychobunny
    wrote on last edited by
    #5

    BTW I meant to say ajaxify.variables.get('foo') but looks like you've figured it out 🙂

    jongarrisonJ 1 Reply Last reply
    1
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to psychobunny on last edited by jongarrison
    #6

    Well, it looks like that way does not work for me:

    ajaxify.variables.get('tickettypes')
    

    but there is lots of good stuff in:

    ajaxify.data
    //and this works:
    ajaxify.data.tickettypes
    

    Also, I was calling some of these variables in jQuery's document load and I needed to switch to this event that I found in the ajaxify.js source (which appears only to fire after jQuery's document load):

    $(window).on('action:ajaxify.contentLoaded', function(event, eventInfo){
         console.log("Is ajaxify available yet?" + ajaxify.data.tickettypes);
    }
    

    Should I not be using this style to access values?

    ajaxify.data.tickettypes
    
    1 Reply Last reply
    0
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by
    #7

    I don't feel like I understand ajaxify well enough to start writing docs just yet, but I take notes and maybe down the road I can write something up.

    You guys seem pretty eager to roll your own stuff. So far it seems like that has worked out very well. I'd be interested in hearing some discussion about the motivations behind doing your own templates.js, translator.js, ajaxify.js, etc...

    I've worked on lots of projects in the past that were just huge piles of every existing popular library and that approach certainly can have its own problems. Finding things in your source code so far has been pretty smooth and so I ask the question about motivation just out of curiosity not criticism.

    julianJ 1 Reply Last reply
    1
  • HolyPhoenixH Offline
    HolyPhoenixH Offline
    HolyPhoenix Anime Lovers
    wrote on last edited by
    #8

    I know nothing about this ajaxify yet, but based on what I am reading you should try

    ajaxify.variables.get('tickettypes')

    Instead of

    ajaxify.data.get('tickettypes')

    jongarrisonJ 1 Reply Last reply
    1
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    replied to jongarrison on last edited by
    #9

    @jongarrison We'd love to discuss it, maybe in a new topic? I'm liking this trend tonight of meta topics 😄

    jongarrisonJ 1 Reply Last reply
    1
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to HolyPhoenix on last edited by
    #10

    @HolyPhoenix Thanks for catching that typo (I edited my comment, just now). I had correctly tried @psychobunny 's approach without luck but then typed it incorrectly.

    I found this post from @baris (https://community.nodebb.org/topic/5942/ajaxify-variables-get-deprecated) that says the .get('name') approach has been deprecated and that this style is current:

    ajaxify.data.tickettypes
    
    1 Reply Last reply
    1
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to julian on last edited by
    #11
    This post is deleted!
    1 Reply Last reply
    1

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development