This situation is a bit odd, I realize (but we are where we are 🙂). We have a “main site” and then “franchise sites” that are just pages (channel entries) that live in the main site.
Maybe an example URL would help explain here: https://main-site-example.com/<franchise-site-name>
Each franchise has some unique information they are able to add/edit via their channel entry. There is a Franchise channel for these entries. I’ve received some requests to be able to include custom scripts on a franchise by franchise basis. For example, some franchise locations want to use a chat widget to communicate with their customers. Some don’t and some even want to use different chat widgets. These are all typically added via a little JS script snippet. I was thinking I could just do some conditional rendering of these scripts (and give the users a place to enter these in the channel entry from in the EE admin), but that isn’t working quite right. And it seems to be kind of sporadic and inconsistent. My suspicion is now that this is a template parse order issue and I’m not sure how to solve it (or if it’s even possible).
So https://main-site-example.com/franchise-one
could have a different chat widget client running than https://main-site-example.com/franchise-two
is running (each of these are channel entries).
Any ideas on what could be going on here (or any potential solutions)? I’m coming up empty.
Thanks in advance.
I don’t think it’s the conditionals or parsing. What may be happening is that EE security measures tend to not like javascript in your entries, because that can be used to do bad things. So under typical settings, it’s going to strip out some of that.
If you ‘view source’ on the page and compare the javascript you see to what they typed in and it’s different? That’s likely what’s happening.
I personally wouldn’t let them add javascript snippets in the content. But what I would do is give them a select field (or 2 or 3) and if they want to add widgets they can add approved widgets. So say they want to add a third party commenting script and you have 2 that seem reasonably secure. Give them a select with Discus and Commento.
THEN you use your conditional. {if comment_widget == 'disqus'}put the disqus js{if:elseif comment_widget == 'commento'}commento js{/if}
This is just safer. And if they want to add something that’s not available, they just put in a request. You can check it out, make sure it’s something that’s reasonably secure and non-horrible, and then you can add it.
There are ways you can let them add their js, but I wouldn’t let them. This approach will be both easier and safer for them.
Doable?
Robins solution is pretty much what I do for similar situations.
Typically I’ll set up any widgets that may be used with one or more select/radio/checkbox fields for the author to say what widget(s) they want to include in the document. Sometimes I’ll add options to a fluid field so the author can choose what position the content displays at. All that’s left to do is write appropriate templates/partials accordingly.
Well, when testing this on a local install (on my machine) things seem to work as I’d expect. It’s when this is on my production server that it is inconsistent. Sometimes the content of the field is loaded and sometimes not and I can’t spot why this is inconsistent. Some franchises are using a custom chat snippet just fine, for example. I’ve attempted to just add some plain text in to that field and inspect the page and I don’t see it. So I don’t think it’s a JS security with EE issue. Though I understand your recommendations above. All of these franchise entries are really only managed by a few people so I’m not super concerned about that (though may enhance this in the future).
So that’s why I was thinking this was an issue with the conditional logic plus rendering order. I can remove JS from the equation and still don’t always get that content to load in the page (I’m inspecting the source in this instance) when I’d expect.
Definitely weird. So, ignoring the js, what I like to do when it’s weird is simplify it down to the absolutely minimum. In this case- blank template, 1 channel entry tag and something really simple like:
{exp:channel:entries limit="10"}
{title} ({field_one})
{if fieldone}YES{if:else}no{/if}
{/exp:channel:entries}
I take it even something like that is differing production vs dev? I’d do that and see if it gives us any hints. I’d also disable extensions for a very quick test- knowing that may break things. I always hate it when it’s only the live site acting up!
But break it down, see if it helps zero in on what’s weird.
I think this issue has surfaced for me again, and I believe I’ve narrowed the issue so wanted to follow up here. I ended up doing what I need to do above in a different way. Just trial and error lead me to a structure that worked. I now need to do a very similar thing and after some more troubleshooting I think I’ve narrowed it down to an issue with layout variables.
I have a layout that is shared throughout the site. One area I have been leveraging layout variables is in the header of the layout. A couple examples of how this is being used is to swap out some contact information if you’re viewing one of the franchise pages (as mentioned above). So we change out the phone number and Facebook username, for example. These are just text fields on the franchise entry, values being set as layout variables, and then those variables are used in the layout. I’ve been doing this for some time and it’s worked without issue.
I was trying to add another layout variable to be used in the layout header and hit the issue. I paired it all the way back to just displaying the text in the header (set via a text input on the franchise entry same as other layout variables that are working). The crux here is that it works just fine when I’m developing locally, but then fails to work when I deploy the changes to our production server. I’ve been really struggling to figure this out at I’m doing the exact same thing already, but I just noticed in the debug panel (on the template tab - https://www.dropbox.com/s/o2enfklmyfnw34q/Screen%20Shot%202020-08-12%20at%209.24.25%20AM.png?dl=0
- should see a franchise_birdeye
…variable there on my local environment) that my new layout variable isn’t being recognized when on the production server. I’ve cleared the template cache on the production installation.
Am I missing something when adding a layout variable? I’m not seeing anything. Totally stumped. Any ideas?
I’d simplify again. Make a blank template, have nothing but the layout tag and define maybe 2 layout variables- including the one that doesn’t get added. Then ‘hello world’. See if the variable is defined. If not- disable all extensions and see if it makes a difference.
It’s definitely very odd.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.