Framework: [Proposal] Parse string with View

Created on 12 Apr 2013  路  8Comments  路  Source: laravel/framework

I missed this function which lets you to parse view from a string directly, for example:

View::parse('Hello {{ $name }}', ['name' => 'John']);

This will give some cool features for parsing string from DB and etc.

Most helpful comment

For those who are looking, I found a solution that works for us. Note: this is built in Laravel 5 on PHP 5.5. The View::file() method appears to have been added in L5.

<?php namespace App\BladeSupport;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\View\Factory;

class BladeRenderer
{

  /**
   * @var
   */
  protected $html;

  /**
   * @var array|null
   */
  protected $data;

  /**
   * @var Filesystem
   */
  protected $files;

  /**
   * @var Factory
   */
  protected $view;

  /**
   * @param      $html
   * @param array|null $data
   */
  function __construct($html, $data = null)
  {
    $this->html = $html;
    $this->data = $data;
    $this->files = app(Filesystem::class);
    $this->view = app('view');
  }

  /**
   * Redner the view
   * 
   * @return \Illuminate\Contracts\View\View
   */
  public function render()
  {
    $this->saveTemporaryHtml();

    $view = $this->view->file($this->getFilePath(), $this->data);

    //$this->deleteTemporaryHtml();

    return $view;
  }

  /**
   * Save the temporary file.
   */
  protected function saveTemporaryHtml()
  {
    $this->files->put($this->getFileName(), $this->html);
  }

  /**
   * Get the temp file name.
   *
   * @return string
   */
  protected function getFileName()
  {
    return md5($this->html) . '.blade.php';
  }

  /**
   * Get the temp file path.
   *
   * @return string
   */
  protected function getFilePath()
  {
    return storage_path('app/' . $this->getFileName());
  }

  /**
   * Delete the temporary file
   */
  protected function deleteTemporaryHtml()
  {
    $this->files->delete($this->getFileName());
  }
}

All 8 comments

sprintf('Hello %s', 'John')?

No, more complex:

@if(Auth::check())
Show some
@endif

And this will come from DB

Hmm interesting idea. I can certainly see this as being useful for rendering views from the DB. It will need a bit of a refactor to allow this method to accept a template as string, likely a second param, or alternate method (better). https://github.com/laravel/framework/blob/master/src/Illuminate/View/Compilers/BladeCompiler.php#L59-L67

You can do it now it's just a bit long winded to get the blade instance.

But it would be cool to have simple command for this, it will open a lot of potential, for example configurable email templates, html blocks etc... I really missed that.

This is somewhat tricky to do without resorting to eval, which I don't want to do. We would have to write the string to disk somewhere and then load it up like a regular view.

I haven't ever used that kind of stuff, but could we try to write to php://memory streams? Could those even be included?

For those who are looking, I found a solution that works for us. Note: this is built in Laravel 5 on PHP 5.5. The View::file() method appears to have been added in L5.

<?php namespace App\BladeSupport;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\View\Factory;

class BladeRenderer
{

  /**
   * @var
   */
  protected $html;

  /**
   * @var array|null
   */
  protected $data;

  /**
   * @var Filesystem
   */
  protected $files;

  /**
   * @var Factory
   */
  protected $view;

  /**
   * @param      $html
   * @param array|null $data
   */
  function __construct($html, $data = null)
  {
    $this->html = $html;
    $this->data = $data;
    $this->files = app(Filesystem::class);
    $this->view = app('view');
  }

  /**
   * Redner the view
   * 
   * @return \Illuminate\Contracts\View\View
   */
  public function render()
  {
    $this->saveTemporaryHtml();

    $view = $this->view->file($this->getFilePath(), $this->data);

    //$this->deleteTemporaryHtml();

    return $view;
  }

  /**
   * Save the temporary file.
   */
  protected function saveTemporaryHtml()
  {
    $this->files->put($this->getFileName(), $this->html);
  }

  /**
   * Get the temp file name.
   *
   * @return string
   */
  protected function getFileName()
  {
    return md5($this->html) . '.blade.php';
  }

  /**
   * Get the temp file path.
   *
   * @return string
   */
  protected function getFilePath()
  {
    return storage_path('app/' . $this->getFileName());
  }

  /**
   * Delete the temporary file
   */
  protected function deleteTemporaryHtml()
  {
    $this->files->delete($this->getFileName());
  }
}
Was this page helpful?
0 / 5 - 0 ratings