List Helper Function
-
I’m trying to register a helper function that works similar to #each, but wraps the the listed item in a UL tag. (I’ve tried using {{#if repeater-control}} but this always returns true even when the repeater is empty and returns no content.)
My code is as follows:
function handlebars_helper_output_list($handlebars) { $handlebars->registerHelper('list', function($context, $options = null) { $return_string = ""; for($i = 0; $i < count($context); $i++) { $return_string .= $options['fn']($context[i]); } if($return_string != "") { $return_string = '<ul">'.$return_string.'</ul>'; } return $return_string; }); } add_action('lzb/handlebars/object', 'handlebars_helper_output_list');
And then in the LazyBlocks block editor, between the {{#list}} and {{/list}} tags looks similar to this output:
<li><a class="{{link-class}}" href="{{link}}">{{link-text}}</a></li>
However, this line is always returning “” even when there are items in the repeater:
$options['fn']($context[i]);
I’m not sure what the issue is. Based on the javascript syntax used for #each, this would be valid, but I also don’t see any mentions of how options.fn behaves when registering the helper as part of functions.php. Any help would be greatly appreciated.
-
Hey.
Just use this check:
{{#if repeater-control.length}} ... {{/if}}
Regards, nK.
Using length still outputs 1 empty record. So if I’m doing:
{{#if repeater_control.length}} <ul> {{#each repeater_control}} <li><a class="{{link-class}}" href="{{link}}">{{link-text}}</a></li> {{/each}} </ul> {{/if}}
I still get just:
<ul></ul>
as an output. It will still return a single blank record and have a length even if none of the properties of the repeater are filled in / if there is no row added to the repeater control. I do have a dropdown control in the repeater as well, the above is kind of truncated to give you an idea of the structure, so my only guess is that if the dropdown had a default value, that value is always present. But it’s strange to me that even if no row is present, e.g.; 0 items have been added to that specific repeater, it always contains at least 1 record.
This sounds like a bug, but I can’t reproduce it locally.
Can you export your block to JSON and place it here? https://lazyblocks.com/documentation/export-blocks/
Sure. The below is the exported JSON from the block I’m using:
[ { "id": 184, "title": "Account Tile", "icon": "<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path d=\"M18 4H6c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 14H6V6h12v12z\" \/><\/svg>", "keywords": [], "slug": "lazyblock\/account-tile", "description": "", "category": "lazyblocks", "category_label": "lazyblocks", "supports": { "customClassName": true, "anchor": false, "align": [ "wide", "full" ], "html": false, "multiple": true, "inserter": true }, "ghostkit": { "supports": { "spacings": false, "display": false, "scrollReveal": false, "frame": false, "customCSS": false } }, "controls": { "control_20ba2b4b9b": { "type": "text", "name": "caption", "default": "", "label": "Caption", "help": "", "child_of": "control_acca27475c", "placement": "content", "width": "50", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_2819eb4ea1": { "type": "url", "name": "link-url", "default": "", "label": "Link URL", "help": "", "child_of": "control_acca27475c", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_53b9df42ca": { "type": "image", "name": "icon", "default": "", "label": "Icon", "help": "", "child_of": "control_acca27475c", "placement": "content", "width": "50", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "preview_size": "medium", "placeholder": "", "characters_limit": "" }, "control_7e29014f6c": { "type": "text", "name": "title", "default": "", "label": "Title", "help": "", "child_of": "", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_9f3aba4c62": { "type": "text", "name": "brief-description", "default": "", "label": "Brief Description", "help": "", "child_of": "", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_3068dc46ff": { "type": "classic_editor", "name": "features", "default": "", "label": "Features", "help": "", "child_of": "", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_64384a444c": { "type": "repeater", "name": "ctas", "default": "", "label": "Calls to Action", "help": "", "child_of": "", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "rows_min": "0", "rows_max": "2", "rows_label": "CTA {{#}} - {{cta-text}}", "rows_add_button_label": "", "rows_collapsible": "true", "rows_collapsed": "true", "placeholder": "", "characters_limit": "" }, "control_76e9474010": { "type": "select", "name": "cta-style", "default": "cta-btn", "label": "Call to Action Style", "help": "", "child_of": "control_64384a444c", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "choices": [ { "label": "Solid Button", "value": "cta-btn" }, { "label": "Ghost Button (Teal)", "value": "cta-ghost-teal" }, { "label": "Half Underlined Link (Teal)", "value": "cta-lnk-teal" } ], "allow_null": "true", "multiple": "false", "output_format": "", "placeholder": "", "characters_limit": "" }, "control_78d9e84efb": { "type": "url", "name": "cta-link", "default": "", "label": "Call to Action Link", "help": "", "child_of": "control_64384a444c", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_bda8a44e00": { "type": "text", "name": "cta-text", "default": "", "label": "Call to Action Text", "help": "", "child_of": "control_64384a444c", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "placeholder": "", "characters_limit": "" }, "control_f488674e92": { "type": "checkbox", "name": "speedbump", "default": "", "label": "Speedbump Link", "help": "", "child_of": "control_64384a444c", "placement": "content", "width": "50", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "checked": "false", "alongside_text": "", "placeholder": "", "characters_limit": "" }, "control_cb8b894441": { "type": "checkbox", "name": "new-tab", "default": "", "label": "Open Link in New Tab", "help": "", "child_of": "control_64384a444c", "placement": "content", "width": "100", "hide_if_not_selected": "false", "save_in_meta": "false", "save_in_meta_name": "", "required": "false", "checked": "false", "alongside_text": "", "placeholder": "", "characters_limit": "" } }, "code": { "output_method": "html", "editor_html": "", "editor_callback": "", "editor_css": "", "frontend_html": "<div class=\"account-tile-single\">\n <div class=\"tile shadow-box\">\n <div class=\"tile-inner\">\n <div class=\"caption\">\n {{#if title}}<h3 class=\"title\">{{title}}<\/h3>{{\/if}}\n {{#if brief-description}}<h4 class=\"subtitle\">{{brief-description}}<\/h4>{{\/if}}\n {{#if features}}\n <div class=\"body\">\n <code>features</code>\n <\/div>\n {{\/if}}\n {{#if ctas.length}}\n <ul class=\"actions\">\n {{#each ctas}}\n {{#if cta-text}}\n <li><a class=\"{{cta-style}}{{#if speedbump}} speedbump{{\/if}}\"{{#if new-tab}} target=\"_blank\"{{\/if}} href=\"{{cta-link}}\">{{cta-text}}<span class=\"visuallyhidden\"> - {{title}}<\/span><\/a><\/li>\n {{\/if}}\n {{\/each}}\n <\/ul>\n {{\/if}}\n <\/div>\n <\/div>\n <\/div>\n<\/div>", "frontend_callback": "", "frontend_css": "", "show_preview": "always", "single_output": false }, "condition": [] } ]
- This reply was modified 2 years, 11 months ago by volaticus.
Aha, I got it. Will be fixed shortly, already preparing the new version ??
Awesome, thank you for your help!
- The topic ‘List Helper Function’ is closed to new replies.