-
Notifications
You must be signed in to change notification settings - Fork 205
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
Code styling to output HTML in PHP #240
Comments
I'm not a fan of jumping in & out of PHP with My preference is for good old printf( trim( '
<select>
<option value="%s">%s</option>
<option value="%s">%s</option>
<option value="%s">%s</option>
</select>' ),
esc_attr_e( $value ), esc_attr_e( $option ),
esc_attr_e( $value ), esc_attr_e( $option ),
esc_attr_e( $value ), esc_attr_e( $option )
); This allows for a template-like syntax and late escaping. |
In terms of unification, I get that it can sometimes be confusing to see different styles, but this isn't the kind of thing I think we should be dictating for our engineers. Like you said, it's largely a matter of personal preference. If we did decide to standardize here, I'd want us to strongly consider adopting a lightweight templating library like Twig which would have additional benefits like automatic escaping. |
That makes sense, but still in absence of a templating library maybe encouraging engineers to use |
+1 from me on recommending using |
respectfully, -1 from me, as it muddies up the works for esc_html__ and esc_html_e, as necessitated by i18n. as for 'jumping in and out of php tags', consider that it at least tends to promote html looking like html... |
@krisgale you can ( and should ) use |
per the above example from @daveross , i see how it Can still be used with printf/sprintf -- i just don't see the real value of Additional function-wrapping. @daveross also per your example, how might that look in a loop? like this?
how is this ^ better than...
? to me one resembles Perl programming, the other resembles html with tags to output values. |
I see what you mean :) I think overall, this is a case of where the code is being used. If we have a template that is predominately HTML, I can see the benefit of the inline style of outputting PHP variables. It does read easier. Ultimately, this is going to be very much the personal preference of the engineer. Maybe if this is included in the Best Practices, we recommend against the echo :allthethings approach, suggest the alternatives and leave it there? |
i would agree that using printf/sprintf to assemble a bunch of html to prepare for output all-at-once further down in the page/template makes sense... with the caveat of being wary of the amount of markup being constructed and memory limitations. |
and react renders both obsolete :-p -- but that's a separate debate. |
My 2 cents, for single line stuff that gets unreadably long I use (s)printf. When using (s)printf you can put the variables on their own lines which increases readability as opposed to the
For html spanning a couple of lines I'd |
I think it really just depends on the amount of HTML being outputted. For a line our two surrounded by PHP, sprintf/printf makes sense. For a large block of HTML, a template file our |
Somewhat tangent to this: if we were to standardize it, I'd prefer the standard to focus more on making sure we minimize the amount of logic needed on templates (by using helper functions to build all data before sending to the template) and the amount of markup built outside of templates. (we already have wording on this, but it could be expanded/more clear) Then we could possibly recommend styles that align better in each case (I'd be in favor of @tlovett1's suggestion above) |
We currently have a section to Avoid Heredoc and Nowdoc as it's impossible to do late escaping and where it's mentioned "engineers should avoid heredoc/nowdoc syntax and use traditional string concatenation & echoing instead".
I would like to know how people feel about how to construct HTML within the PHP code, echoing vs open/close php tags to include the HTML.
Across projects I see both different ways of doing it, I assume because it's a matter of personal preference.
But I do think
is more readable than echoing
Also in the way code editors highlight different code languages.
Although this is a very basic example, I have seen cases where it can get very messy and it would be nice to have some kind of unification across projects.
The text was updated successfully, but these errors were encountered: