Peter Knight Posted September 1, 2015 Share Posted September 1, 2015 (edited) I noticed some example code provided recently had a very simple API format as follows: <a href='{url}'>{title}</a> Since I started using PW I've always preceded my field name with a variable and thought this was best practice. <a href='{$item->url}'>{$item->title}</a> What's the general advice here? Looking at the forums and blog I can see mention of a shorter syntax mentioned here https://processwire.com/blog/posts/processwires-roadmap-in-2015/ But then I can also see a post which seems to indicate that short tag-like syntax isn't necessarily a good thing https://processwire.com/api/why-php-syntax/ Edit: changed "proceeded" to "preceded" Edited September 1, 2015 by Peter Knight Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 1, 2015 Share Posted September 1, 2015 There's a fundamentally difference between your example strings. The first one is using tags, that are mostly used in conjunction with WireArray::implode and WireArray::explode or in newer versions you can also use them in the backend to create custom pageList labels in templates. These use the wirePopulateStringTags() helper function, that does replace the tags with their value out of a known or provided context. Nothing as fancy as a templating language, but nice for simpler usage. The second one is plain php, but the curly braces are actually optional as long as you're just calling a direct property of the variable. If you want to call deeper nested properties, then you need the curly braces (e.g. "<span>{$page->parent->title}</span>"). One last notable difference, while the tags are just strings, and therefore work no matter how they are created, the php string interpolation does only work when using double quotes. 2 Link to comment Share on other sites More sharing options...
Peter Knight Posted September 1, 2015 Author Share Posted September 1, 2015 Thanks for clarifying @LostKobrakai Something I'd been meaning to ask for a while. Link to comment Share on other sites More sharing options...
Raymond Geerts Posted September 1, 2015 Share Posted September 1, 2015 In general when i have to use a field value more than once i tend to set it to a variable and use that. I like to keep my code as minimal as possible. Link to comment Share on other sites More sharing options...
ryan Posted September 1, 2015 Share Posted September 1, 2015 I prefer to use PHP variables whenever possible. But there are some cases where it wouldn't be applicable or possible, like with a call to WireArray::implode(), etc. If there's an instance where you need to supply an output format (like with markup) before the variables are known/available, then the only way to do it is to {tag} it with placeholders. This is useful when providing an output format for something ahead of time that's going to be repeated over many times. For instance, the supporting markup for PW's Inputfield forms is generated in this manner, so that a module or core can specify the output format without having to get involved in the runtime rendering of the form. But it's not really a question of which strategy you should use, because most of the places where we support {tags} using variables isn't a possible alternative. 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now