[Views] Using dwoo template to show checkbox and radio button states


#1

What is the general practice way to use a dwoo template to render checkboxes and radio buttons that already have a state?

I’m using a serialized array to save the user’s PrivacySettings, here’s the Model:

User [
    PrivacySettings => [
           'email-notifications'          => false,
           'newsletter-notifications'  => true,
    ]
]

In my template:

 {checkbox value=true inputName="PrivacySettings[notification-setting]" default=$User->PrivacySettings['email-notifications'] label="Allow site to send me messages"}

Is there another step I’m missing to access the $User->PrivacySettings property? Apologies if this is a really dumb question, my experience is in Twig and Blade. Thank you!


#2

Do you have this setup somewhere I could take a look and try debugging? Looks generally right but there might be a minor syntax issue

Also I recommend using json columns instead of serialized columns as they provide the same functionality in cases like this but the JSON is a lot easier to work with if you ever have to pull the data into something that’s not PHP

The template engine is Dwoo v1 BTW, not Smarty – its inspired by Smarty but stripped down a bit and does away with the notion of “sandboxing” templates to prevent direct access to arbitrary PHP code – so it’s more of a syntax sugar than a DSL


#3

Thanks, didn’t know json was a serialization option, I’ll switch to that!

The view file is html-templates/profile/profileEdit.tpl in the http://staging.cities2night.clients01.jarv.us/ repository. I tried using {html_checkboxes} to generate the 3 checkboxes, but that was when I thought we were using Smarty. Will read up on the Dwoo docs.


#4

I repaired your issue, it mostly looked good. Due to the nature of HTML checkboxes, by default if the checkbox is unchecked nothing is sent. Emergence’s endpoints typically treat unset fields as “no change”, so you didn’t have any way to post that the setting should be changed to false if its unchecked rather.

The {checkbox} form template has an option unsetValue which when set will generate a hidden input before the checkbox that has the name name but uses the unset value. This way if the checkbox is checked, the value gets posted, but if its unchecked the unsetValue from the hidden field will be posted instead of the field being omitted.

You were also needlessly wrapping its output in label tags when label tags were already included


#5

Excellent, thank you for the tips. I changed each checkbox to have a unique name in the PrivacySettings array so the behavior works as expected. I’ll add a section in the wiki, in the meantime I’ve discovered that the {radio} tag isn’t available, I couldn’t find an example in this code base. Could I have an example from another project?


#6

Well #1 you can just make the markup yourself if you want, the form subtemplates are just helper shortcuts not a required way to build forms.

You could use the {refill} plugin with your radios like <input type="radio" name="foo" value="bar" {refill field=foo checked=bar default=bar}

Doesn’t look like anyone ever made one for radios, might be possible just to make the “inputType” for the checkbox one be a template parameter that defaults to checkbox but can be set to radio