Extending Blade in Laravel - Creating a custom form object -


the documentation on extending blade in laravel 5 doesn't seem have depth , i'm struggling understand if want possible.

note: using laravel 5.1

this example laravel docs give extending blade.

blade::directive('datetime', function($expression) {     return "<?php echo with{$expression}->format('m/d/y h:i'); ?>"; }); 

now want have blade file looks this:

@form('horizontal')     {{ $form->text('email') }}     {{ $form->text('password') }}     {{ $form->submit }} @endform 

then want new $form object created whenever use @form , have available use after use @form doesn't need available after @endform

after bit of googling , messing around have found can this:

\blade::directive('form', function($expression) {     return '<?php $form = new app\form'.$expression.'; ?>     <?php echo $form->open(); ?>'; }); 

(this updated previous edit)

that's of way there. have $form object , can create new form , can send custom param form object. can use $form object after @form. of course available throughout view, that's not perfect, not problem.

however since seem doing undocumented fear may doing wrong, or may run problems later.

i should note every single guide have found online refer $compiler->creatematcher , $compiler->createplainmatcher both of these seem removed in laravel 5.1.

can offer insight how blade extensions work , suggest better ways of doing this? right seems work about, feels 'hacky' , perhaps not safe rely on going forward.

update html macros

to update on use case based on answer below suggesting html macros. css framework bootstrap includes different formatting vertical , horizontal forms. purpose of functionality make easy switch between these two, example below:

@form('horizontal')     {{ $form->text("email", $user->email, 'email') }} @endform 

the form class might this:

class form {     public $direction;      public function __construct($direction)     {         $this->direction = $direction;     }      public function text($name, $value, $label)     {         return $this->wrap('<input type="text" value="'.$value.'" />', $name, $label);     }      public function wrap($element, $name, $label)     {         if($this->direction == 'horizontal') {             //horizontal             return '<div class="form-group">                 <label class="col-sm-2 control-label" for="'.$name.'">'.$label.'</label>                 <div class="col-sm-10">                     '.$element.'                 </div>             </div>';         } else {             //vertical             return '<div class="form-group">                 <label for="'.$name.'">'.$label.'</label>                 '.$element.'             </div>';         }     } } 

as far i'm aware html macro's cannot achieve sort of thing.

this pseudo example code, apologies errors. believe basic premise of should clear though.

you might want plugin "illuminate/html": "~5.0".

you can things open forms these 2 method do.

{!! form::open() !!}  {!! form::model($pose,['method' => 'patch', 'route' => ['poses.update', $pose->id]]) !!} 

as bonus many other items.

{!! form::text('title', null, [            'class' => 'form-control',             'id' => 'title', 'placeholder' => 'add title.'           ]) !!} {!! form::textarea('body', null, [            'class' => 'form-control',             'id' => 'body', 'placeholder' => 'post status'           ]) !!} {!! form::close() !!} 

and many others, see source.

as far original question, can use 5.0 function , put original method createopenmatcher in service provider. see answer here.

public function createopenmatcher($function){     return '/(?<!\w)(\s*)@'.$function.'\(\s*(.*)\)/'; } 

update per comment

so per comment, illuminate form helper asking.

{{ form::open(['class' => 'form-horizontal']) }} 

which renders as

<form method="post"      action="http://example.com/sample"      accept-charset="utf-8"      class="form-horizontal"> <input name="_token"      type="hidden"      value="zqvwyzxxkh6shlkpdjjvajp0s6bg5bxeua1rconk"> 

anything passed array added form tag.

i might not understand question 100%, think may trying solve blade extensions might easier solve code editor or form helper plugin. maybe add 2 types of html trying generate , discuss that?

i use both phpstorm , sublime text. both of these have options can include code snippets of text. in phpstorm called live templates , sublime calls them snippets.

i use textfieldtab live template phpstorm text fields.

<!-- $value$ field --> <div class="form-group">     {!! form::label('$name$', '$value$:') !!}     {!! form::text('$name$', null, [                'class' => 'form-control',                 'id'=>'$name$',                 'placeholder' => '$value$'      ]) !!}     {!! $errors->first('$name$', '<span class="error">:message</span>') !!} </div> 

which gives render html

<div class="form-group">         <label for="sample">sample:</label>         <input class="form-control"                 id="sample"                 placeholder="sample"                 name="sample"                 type="text">  </div> 

although extend blade time time. have found majority of times, when want extend it, there easier way.

final edit

why don't use css?

<style>     form.horizontal .col-sm-10, form.horizontal .col-sm-2{         display:inline-block;     } </style>  <form class="horizontal">     <div class="form-group">         <label class="col-sm-2 control-label" for="field_name">enter name:</label>         <div class="col-sm-10">             <input type="text" value="new name" />         </div>     </div> </form> <form class="vertical">     <div class="form-group">         <label class="col-sm-2 control-label" for="field_name">enter name:</label>         <div class="col-sm-10">             <input type="text" value="new name" />         </div>     </div> </form> 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -