Giter Site home page Giter Site logo

Comments (4)

RicLeP avatar RicLeP commented on September 26, 2024

Hi @chaseconey

If I’m reading this correctly you basically want to get a JSON representation of the Block? There are couple of variations to that:

  • JSON just like Storyblok provides
  • JSON of content fields (potentially with mutators?)
  • JSON of the block, with the package specific data (or sub set of) and the content

It’s definitely something I’ve thought about, being able to send JSON to the frontend more easily. There isn’t any way to handle any of the above yet.

Blocks could have a toJSON method - and would this attach meta data? Maybe mapping the fields you want in the JSON.

Or something like Laravel’s API Resources? I’ve not used them much so am not that familiar with their pros and cons.

There could also be a benefit to being able to stop parsing the nested blocks and keeping some as JSON. Maybe a nested block doesn’t need to be transformed into a class and simply becomes a JSON or array ‘field’ on the parent?

Sorry, not really answering anything for you here, but is this the kind of thoughts you were having?

from laravel-storyblok.

RicLeP avatar RicLeP commented on September 26, 2024

Hi @chaseconey, do you happen to know exactly what Laravel is doing under the hood with calling @json'? I can’t see where this directive defined in the framework. I’ve been playing with a couple of ideas but need to know what @json` does.

from laravel-storyblok.

czuniga9 avatar czuniga9 commented on September 26, 2024

@RicLeP looking at a view generated in storage/framework/views/, I can see that @json($foo) generates <?php echo json_encode($foo, 15, 512) ?>;.

The 15 being these flags: JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT.

Edit: It is defined in Illuminate\View\Compilers\Concerns\CompilesJson

from laravel-storyblok.

RicLeP avatar RicLeP commented on September 26, 2024

Thanks @czuniga9 ! I didn’t think to look it the compiled view 😅

In 2.10.6 Block now implements JsonSerializable, see: https://www.php.net/manual/en/class.jsonserializable.php

By default it uses $this->content(). If you need something more bespoke then you’ll need to override the jsonSerialize() method on your Block.

I did something similar recently before implementing JsonSerializable which should be easy to migrate to the new style.

class Nav extends BaseBlock
{
    public function toArray() {
	return $this->content()->transform(function ($item) {
		return $item->transform(function ($item) {
			return $item->toArray();
		});
	})->first();
    }
}
class NavDropdown extends BaseBlock
{
	public function toArray() {
		$dropDown =  $this->content()->transform(function ($item) {
			if (is_string($item)) {
				return $item;
			}

			return $item->transform(function ($item) {
				return $item->toArray();
			});
		});

		$dropDown['isOpen'] = false;

		return $dropDown;
	}
}
class NavLink extends BaseBlock
{
	public function toArray() {
		return $this->content()->transform(function ($item) {
			return is_string($item) ? $item : rtrim((string) $item, '/');
		});
	}
}

With the structure:

image

from laravel-storyblok.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.