Stillat / blade-parser

A library that makes it simple to parse, analyze, and manipulate Blade templates. Designed with tool makers in mind ❤️
https://stillat.com/blade-parser
MIT License
125 stars 2 forks source link

Support for SectionNode #15

Closed khalyomede closed 1 year ago

khalyomede commented 1 year ago

Currently @section() is a DirectiveNode

I have to manually get the DirectiveNode::sourceContent property and work with it

I would have expected to have a SectionNode so I could get all node of that type using

$sections = $document->allOfType(SectionNode::class);

And to be able to have all their parameters

$sections = $document->allOfType(SectionNode::class);

$sections->each(function(SectionNode $section): void {
  $parameters = $section->getParameters();

  $parameters->each(function(ParameterNode $parameter): void {
    dump($parameter->value);
  });
});

My goal is to be able to get translations out of the second parameter (if there is any) for my package at https://github.com/khalyomede/laravel-translate from this kind of blade code:

@extends("layout.logged")

@section("title", __("List of books"))

@section("content")
  {{-- ... --}]
@endsection

To be able to pull List of books and add it on the [lang].json file if not added yet

JohnathonKoster commented 1 year ago

Hi there, I have no desire/plans to create custom node types for each individual directive.

You are free to use the existing collection helper methods to retrieve all section directives from the template:

<?php

use Stillat\BladeParser\Document\Document;

$template = <<<'BLADE'
@section ('title')
@section ('title2')
BLADE;

$doc = Document::fromText($template);

$sections = $doc->getDirectives()
    ->where('content', 'section');

Regarding the parameters, similarly this is not something I am planning on supporting at the moment (since it would require including a heavy dependency, as well as not cover scenarios where user's are supplying JSON content to directives). Please see our earlier discussion on ways to do this: https://github.com/Stillat/blade-parser/discussions/12

JohnathonKoster commented 1 year ago

@khalyomede

Also just a reminder about the arguments getValues() method that was added previously to help with this 🙂:

https://github.com/Stillat/blade-parser/releases/tag/v1.1.0