# Craft Twig A Craft CMS influenced Twig bundle for Sublime Text and Textmate, customized to work with Craft CMS specific Twig extensions. [![Craft-Twig Demo Video][image-1]][1] ## Features Craft Twig provides syntax highlighting for Craft templates, several snippets available via tab trigger, a couple key bindings, and comment support. ### Twig Tags (via key bindings) ctrl+shift+[ {{ }} ctrl+shift+5 {% %} command + / {# #} ### Twig Tags (via tab trigger) }} {{ }} %% {% %} ## {# #} do {% do ... %} extends {% extends "template" %} from {% from "template" import "macro" %} import {% import "template" as name %} importself {% import _self as name %} inc, include {% include "template" %} incp {% include "template" with params %} inckv {% include "template" with { key: value } %} use {% use "template" %} autoescape {% autoescape "type" %}...{% endautoescape %} block, blockb {% block name %} ... {% endblock %} blockf {{ block("...") }} embed {% embed "template" %}...{% endembed %} filter, filterb {% filter name %} ... {% endfilter %} macro {% macro name(params) %}...{% endmacro %} set, setb {% set var = value %} spaceless {% spaceless %}...{% endspaceless %} verbatim {% verbatim %}...{% endverbatim %} if, ifb {% if condition %} ... {% endif %} ife {% if condition %} ... {% else %} ... {% endif %} for {% for item in seq %} ... {% endfor %} fore {% for item in seq %} ... {% else %} ... {% endfor %} else {% else %} endif {% endif %} endfor {% endfor %} endset {% endset %} endblock {% endblock %} endfilter {% endfilter %} endautoescape {% endautoescape %} endembed {% endembed %} endfilter {% endfilter %} endmacro {% endmacro %} endspaceless {% endspaceless %} endverbatim {% endverbatim %} ### Twig Tags, Customized for Craft (via tab trigger) The following tab triggers output a simple example of a loop for their respective Craft tags. asset craft.assets.one() assets craft.assets loop categories craft.categories loop entries craft.entries loop feed craft.feeds.getFeedItems loop tags craft.tags loop users craft.users loop cache {% cache %}...{% endcache %} children {% children %} exit {% exit 404 %} header {% header "HEADER" %} hook {% hook "name" %} ifchildren {% ifchildren %}...{% endifchildren %} matrix, matrixif Basic Matrix field loop using if statements matrixifelse Basic Matrix field loop using if/elseif matrixswitch Basic Matrix field loop using switch nav {% nav item in items %}...{% endnav %} paginate Outputs example of pagination and prev/next links redirect {% redirect "login" %} redirectinput {{ redirectInput("url") }} requirelogin {% requireLogin %} requirepermission {% requirePermission "spendTheNight" %} switch {% switch variable %}...{% endswitch %} // Paths alias alias("@baseUrl/images/image.png") svg svg("@webroot/assets/svgs/icon.svg") // CSS/JS css {% css %}...{% endcss %} js {% js %}...{% endjs %} registercssfile {% do view.registerCssFile ("/resources/css/global.css") %} registerjsfile {% do view.registerJsFile("/resources/js/global.js") %} // Output Helpers csrf {{ csrfInput() }} head {{ head() }} beginbody {{ beginBody() }} endbody {{ endBody() }} // craft.app.request getparam craft.app.request.getParam() getbodyparam craft.app.request.getBodyParam() getqueryparam craft.app.request.getQueryParam() getsegment craft.app.request.getSegment() // Settings app craft.app.SETTING config craft.app.config.general.SETTING getenv getenv("VARIABLE") // Sites and Locales ismultisite craft.app.isMultiSite language craft.app.language locale craft.app.locale alllocales craft.app.i18n.allLocales applocales craft.app.i18n.appLocales editablelocaleids craft.app.i18n.editableLocaleIds editablelocales craft.app.i18n.editableLocales getlocalebyid craft.app.i18n.getLocaleById(1) primarysitelocale craft.app.i18n.primarySiteLocale sitelocaleids craft.app.i18n.siteLocaleIds sitelocales craft.app.i18n.siteLocales // Closing tags case {% case "value" %} endcache {% endcache %} endifchildren {% endifchildren %} endcss {% endcss %} endjs {% endjs %} endnav {% endnav %} ### Craft Twig Functions (via tab trigger) ceil ceil() classname className(${1:object})$0 clone clone(${1:object})$0 floor floor() max max() min min() round round() shuffle shuffle() siteurl, siteurla siteurl('path'), siteurla('path', params, 'https', siteIds) url, urla url('path'), url('path', params, 'https', false) ### Debugging dump
{{ dump() }}### PHP dd Craft::dd(); ---- ## Installation TextMate, and most editors that support TextMate bundles, allow the installation of bundles simply by extracting an archive or cloning the repository into the application's bundle directory. This bundle is no different. Below is a list of common bundle directories. #### Sublime Text To install this bundle in Sublime Text, download the bundle via Package Control or, if you wish to install the theme manually, a few extra steps are required: 1. Open Sublime Text and in the Preferences menu click `Browse Packages`. 2. Copy `Craft-Twig.tmbundle` into the Packages folder 3. Restart Sublime Text. **A note on upgrading** If you are upgrading to a new major version (e.g. v3 => v4) you may run into a few errors having to do with cache or sessions. If you run into errors, try the following (this example use paths for OSX and Sublime Text 3): 1. Uninstall your previous version of Craft-Twig 2. Delete any cached version of Craft-Twig in