Jump to content

alekstrust

Members
  • Posts

    2
  • Joined

  • Last visited

Reputation Activity

  1. Like
    alekstrust reacted to Tyson in Get billing contact   
    You can use the client ID to fetch the billing contacts, e.g.
    <?php public function buildProcess(array $contact_info, ...) { Loader::loadModels($this, ['Contacts']); if ($this->ifSet($contact_info['client_id'])) { // Fetch all billing contacts for this client $billing_contacts = $this->Contacts->getAll($contact_info['client_id'], 'billing'); } }  
  2. Like
    alekstrust reacted to NextToNothing in Twig?   
    Hey All,
    Coming from a background in developing with Symfony, I've used Twig a lot. Also used templating engines such as smarty and mustache/handlebars - but I believe Twig to be the better of the bunch.
    I've seen there was a little discussion about Twig in late 2016 and mid 2017. However, I thought it might be a nice idea to have a constructive discussion about Twig and Blesta?
    Even if the end decision was not to use Twig, the discussion might help improve the templating system. Most notably for new-comers to Blesta from competitors or a background like mine (Symfony/Twig or Laravel/Blade).
    I'll start off with a few points:
    Conciseness
    Personally, I believe Twig to be more easy to read and follow the logic, given it's brevity.
    Also, since it uses braces ({}) and not angled brackets (<>), it helps give a contrast between template code and the actual HTML. All this helps readability, which can speed up development and maintainability.
    Arguably, the same could be achieved in PHP via short tags like <? ?> and <?=$var?> - but I believe you may need to enable them in PHP, which is probably why Blesta isn't using them already (given the self-hosted nature). <div class="row<?php echo (!$this->Html->ifSet($show_header, true) ? ' login' : '');?>"> <?php if (!empty($active_nav['secondary'])) { ?> <div class="col-md-3"> <div class="list-group"> <?php foreach ($active_nav['secondary'] as $link => $value) { ?> <a href="<?php $this->Html->_($link);?>" class="list-group-item borderless left-nav <?php echo ($value['active'] ? 'active' : '');?>"> <i class="<?php $this->Html->_($value['icon']);?>"></i> <?php $this->Html->_($value['name']); ?> </a> <?php } ?> </div> </div> <div class="col-md-9"> <div class="row"> <?php echo $content;?> </div> </div> <?php } else { echo $content; } ?> </div>
    versus...
     
    <div class="row{% if not show_header %} login{% endif %}"> {% if active_nav.secondary is not empty %} <div class="col-md-3"> <div class="list-group"> {% for link, value in active_nav.secondary %} <a href="{{ link|e }}" class="list-group-item borderless left-nav {% if value.active %}active{% endif %}"> <i class="{{ value.icon|e }}"></i> {{ value.name|e }} </a> {% endif %} </div> </div> <div class="col-md-9"> <div class="row"> {{ content }} </div> </div> {% else %} {{ content }} {% endif %} </div> (I didn't test this Twig code, just to confirm)
     
    Control Structure Syntax
    This kind of comes into conciseness, but is only really for PHP.
    It might be beneficial to use the alternative syntaxes, instead of braces {} in the pdt templates. It would certainly help me get less confused when reading large chunks  
    http://php.net/manual/en/control-structures.alternative-syntax.php
      IDEs
    I use PHPStorm, and it has features for Twig, such as better highlighting, auto-completes, etc. With pdt templates being literally PHP, I had to just set them to plain-ol' PHP files - meaning no helpers from the IDE that can speed up development. 
      Inheritance, blocks
    I think this is probably quite a big point, as blocks are more-or-less how the templating engine in Blesta works - except it's all in the core code.
    Again, it helps readability - given that from just the template file, I know all of the templates that are gonna be executed.
    As an example, it can also be useful if someone wanted to change other code on specific pages. In Twig, I'd be able to edit just the one template file - however, with the current system, I'd have to edit core files, which would end up being a pain with upgrades overwriting the files. {% extends "structure.html" %} {% block content %} <b>Content of the page...</b> {% endblock %} opposed to just having a file on it's own with the page content, and having to rely on the core code to define everything else: <b>Content of the page...</b> Extensibility
    Obviously, PHP is PHP, and you can do what you like with creating functions etc (however, loading them into the template engine is probably another dicussion).
    Twig is very versatile in the respect.
    For example, phpBB made the switch from their own engine to Twig, and was able to code extensions to Twig to allow all their old templates to run under Twig.
    https://www.phpbb.com/community/viewtopic.php?f=461&t=2424606
      Everything else over on the Twig homepage -https://twig.symfony.com/  
    Obviously, it'll involve a bit of work to move over to Twig, but I feel it would be beneficial in the long run, help new-comers, and more importantly help grow the development of themes for Blesta.
    Given Blesta has a template engine orchestrating everything, I might have a little play around with integrating it myself. ?

    Hopefully we get a good discussion going on the topic?
     
    sidenote - I noticed that templates for plugins can't get overwritten in the app/views/ directory? Thought this was a bit odd, given that most template files have a unique name. So, could we possibly check the app/views/ first before going back to the plugin default. Personally, I wanted to edit a template file in the support_manager plugin but keep all my theme files in one central location (with Git). Unfortunately, I think that I might have to end up with template files here there and everywhere? :S
     
  3. Like
    alekstrust reacted to NextToNothing in Twig?   
    I agree the current way of templating does the job. However, it is a bit cumbersome, and in some cases can be limiting with the the whole "pages expected to be rendered within the structure.pdt file" - opposed to actual inheritance.
    When I started coding the template files, it thrown be back a good few years back to the days of procedural PHP code.
    Twig is more OO in it's design. It makes heavy use of inheritance and trait-like blocks/includes. It just makes more sense to utilise such features. 
    I mean, you could write an entire application in procedural PHP code today, and it'll do the job, yeah - but there are reasons we choose to code in OO with classes etc.
    Personally, I think a good metaphor is that; templating (like Twig) is the same to PHP, that a "preprocessor" (like Sass/Less) is to CSS.
    You can make your code adhere more to DRY principles, by utilising thing like mix-ins and inheritance. Not to mention all the other useful features such as auto-escaping, etc.
×
×
  • Create New...