-
-
Notifications
You must be signed in to change notification settings - Fork 596
Values Blade Wrapper #5201
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
Merged
Merged
Values Blade Wrapper #5201
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Previously it would only handle when they're not wrapped in a Value object. The first test was coverage for the existing thing, the second test is coverage for the fix.
Use mocks for test so it's less reliant on a real tag, and the return values can be more easily tested for augmentation etc
|
…I am running out of emojis trying to express the excitement about this PR 🤩 |
This reverts commit 680dfb1.
# Conflicts: # src/Fieldtypes/Entries.php
3 tasks
jasonvarga
added a commit
to statamic/docs
that referenced
this pull request
Feb 14, 2022
jackmcdade
added a commit
to statamic/docs
that referenced
this pull request
Mar 15, 2022
* New Antlers docs (#692) Co-authored-by: Jason Varga <[email protected]> * Update blade doc with new `Statamic::tag()` and `Statamic::modify()` usage. (#702) * Frontend form field conditions (#691) * Document conditional fields in front-end forms. * JS. * Finish documenting custom JS drivers. * Suggest more real-world example gist, as well as a link to our built-in Alpine driver. * Tweak the intro Co-authored-by: Jack McDade <[email protected]> * Link to the new parser docs * Mention alternate closing tags * Refactor JSON encoding for form attributes in JS drivers (#723) * fix mobile header overflow issue. Closes #725 * Document newest PHP delimiter * Show the new new site site in the quick start guide * Delete installed.png * initial 3.3 upgrade guide * Blade variables as per statamic/cms#5201 * fix a few code examples * change per statamic/cms#5302 * Improve docs on getting data out of entries * reword * avoid the slash so the example isn't highlighted as a regex. its just an example. * wip * Rely on the automatic TOC * Update fluent tag docs with param setters (#739) * zero impact 3.3 changes * fetch and pagination example * property access * explain void * form submission data change * live preview * live preview * Remove live preview extending page - it's all covered in the regular one * at symbol can escape braces in strings and params * Laravel 8 * explain date field change * Add date where clauses * laravel upgrade guide * Tweak the L7-L8 upgrade guide * A few more tweaks * fix tpyo * enh? * Improve Blade docs * Remove caveats. * Routes and Controller examples for Blade * A little more detail up front Co-authored-by: Jason Varga <[email protected]> Co-authored-by: Jesse Leite <[email protected]>
superstar1205
added a commit
to superstar1205/lc
that referenced
this pull request
Aug 21, 2022
* New Antlers docs (#692) Co-authored-by: Jason Varga <[email protected]> * Update blade doc with new `Statamic::tag()` and `Statamic::modify()` usage. (#702) * Frontend form field conditions (#691) * Document conditional fields in front-end forms. * JS. * Finish documenting custom JS drivers. * Suggest more real-world example gist, as well as a link to our built-in Alpine driver. * Tweak the intro Co-authored-by: Jack McDade <[email protected]> * Link to the new parser docs * Mention alternate closing tags * Refactor JSON encoding for form attributes in JS drivers (#723) * fix mobile header overflow issue. Closes #725 * Document newest PHP delimiter * Show the new new site site in the quick start guide * Delete installed.png * initial 3.3 upgrade guide * Blade variables as per statamic/cms#5201 * fix a few code examples * change per statamic/cms#5302 * Improve docs on getting data out of entries * reword * avoid the slash so the example isn't highlighted as a regex. its just an example. * wip * Rely on the automatic TOC * Update fluent tag docs with param setters (#739) * zero impact 3.3 changes * fetch and pagination example * property access * explain void * form submission data change * live preview * live preview * Remove live preview extending page - it's all covered in the regular one * at symbol can escape braces in strings and params * Laravel 8 * explain date field change * Add date where clauses * laravel upgrade guide * Tweak the L7-L8 upgrade guide * A few more tweaks * fix tpyo * enh? * Improve Blade docs * Remove caveats. * Routes and Controller examples for Blade * A little more detail up front Co-authored-by: Jason Varga <[email protected]> Co-authored-by: Jesse Leite <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces a new
Valuesclass that will make working with Blade a lot nicer.This class will wrap an array/collection and make accessing data available as if it were an Eloquent model.
The class will be returned by the top level
pagevariable as well as tags, and you will interact with it without needing to ever really think about it.Basically, no more
$value = $value instanceof Value ? $value->value() : $value. Just$value.Closes #1446
What it's fixing
In a Blade template, all of the entry's "augmented" values are exposed to you as variables.
e.g. If your entry's blueprint has an
entriesfield namedrelated_posts, it would be aValueinstance with a raw value of a list of IDs. You'd be able to get the entries by calling->values(). We do this to make the augmentation happen lazily, for performance.In Antlers, it's automatic. In Blade, you have manually call
->values()which is annoying. And since thoseValueobjects are mixed in with regular non-Value objects, you only have to do it sometimes, which is even more annoying.Echoes / Automatic casting
If you were just echoing a value, you could do that without worrying.
This would output the augmented value because
Valueknows how to cast to a string.Conditions / Manual casting
But for situations where it wouldn't automatically cast, you would need to know if you're working with a
Valueobject, and manually call->value()on it.If you were doing a conditional check on a field from your blueprint, you'd need to get the underlying value.
This would always be
true, even if the value wasfalse, becauseif (Value instance)evaluates totrue.You'd need to do this:
But if it's not a
Valueinstance, you wouldn't have to call->value()....If you added
->value()it would say something likeCall to a member function value() on bool.Again, it's silly to have to figure out whether you are or aren't working with a
Valueeach time, and call an extra->value()method on it.Act like a model
Now, the top level
pagevariable, as well as any globals will be instances of this newValuesclass.Instead of using the top level fields, you can get them through the
$pagevariable.If any of those are
Valueobjects, it'll grab the augmented version.If for some reason you need the "raw" value (spoiler: you probably never do) you can call
$entry->raw('foo').Note that other global-ish variables are still available at the top level:
The new
Statamic::tag()helper will be returning instances of these newValuesclasses too, allowing you to do the same thing within loops:Queries
Similar to Eloquent relationships, you may chain query methods by using a method, but calling a property will resolve the query and give you a collection.
For example, let's say you have a
related_postsfield, which is anentriesfield.String/JSON casting
If you try to echo a field that you expect to be an array or collection, it will convert it to JSON.
Same goes for the
@jsonor@jsdirectives.