Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Twig switch/case extension #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add Twig switch/case extension #6

wants to merge 1 commit into from

Conversation

codechefmarc
Copy link
Collaborator

@codechefmarc codechefmarc commented Sep 5, 2024

Summary

Adds a new switch/case extension to Twig allowing for switch/case from within templates.

This PR fixes/implements the following bugs/features

  • Adds a new Twig extension for switch/case statements
  • Updates README.md with documentation on how to use this new feature

Explain the motivation for making this change. What existing problem does the pull request solve?

Continue adding new functionality to Emulsify Tools.

Documentation update (required)

Yes. README.md was updated with new information also copied here for eventual inclusion on the Emulsify website:

This adds the ability to do a switch/case function from within Twig templates. To use:

{% switch content.field_name.0 %}
    {% case "text" %}
      <p>This appears if the field name value is set to "text"</p>
    {% case "image" %}
      <p>This appears if the field name value is set to "image"</p>
    {% default %}
      <p>The field text did not match any case.</p>
{% endswitch %}

Note that the switch, endswitch, and case tags are required and the default is optional.

How to review this pull request

  • Add the following to the composer.json repositories section:
{
  "type": "vcs",
  "url": "https://github.com/emulsify-ds/emulsify_tools.git"
}
  • Go into your modules folder and delete any existing emulsify_tools that are already there
  • From the root of the project, run composer require emulsify-ds/emulsify_tools --prefer-source
  • Go into the directory where emulsify_tools now resides
  • Change to this branch git checkout switch-case
  • Make sure emulsify_tools is installed
  • Go into a Twig template and write something like this:
{% set variable_name = "item 1" %}

{% switch variable_name %}
  {% case "item 1" %}
    Do something here only if the variable is set to "item 1"
  {% case "item 2" %}
    item 2 things
  {% default %}
    This is default if the variable doesn't match
{% endswitch %}
  • Verify that the switch/case statement works as intended
  • Change the variable to "item 2" and verify that the output is different
  • Verify that by removing the optional default tag, that the function still runs as intended
  • Test other uses cases such as: from within a for loop

Copy link

@joetower joetower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codechefmarc This is awesome, great work! Everything appears to work as you've outlined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants