Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sebastian De Deyne:
Server side rendering JavaScript from PHP
Mar 13, 2018 @ 15:21:24

Sebastian De Deyne has a tutorial posted to his site showing you how to use two packages to render Javascript on the server side via PHP.

Server side rendering is a hot topic when it comes to client side applications. Unfortunately, it's not an easy thing to do, especially if you're not building things in a Node.js environment.

I published two libraries to enable server side rendering JavaScript from PHP: spatie/server-side-rendering and spatie/laravel-server-side-rendering for Laravel apps.

Let's review some server side rendering concepts, benefits and tradeoffs, and build a server renderer in PHP from first principles.

The tutorial starts with a look at what "server-side rendering" means in this case, how it works with the browser and some of the benefits. It then talks about the rendering process and some of the tradeoffs between it and just using Javascript on the frontend. From there the post gets to the examples of the actual rendering process. If creates a simple Vue.js application that outputs a "Hello, world!" template. It then includes the code to use the packages (that, in turn, use the V8JS package) to render the Javascript and output the result.

tagged: serverside render javascript tutorial package spatie v8js

Link: https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php

Symfony Finland:
Attaching React.js to a template rendered on the server with Twig
Nov 13, 2017 @ 15:27:49

On the Symfony Finland blog there's a new tutorial posted sharing the results of their effort to get React.js to play nice with Twig, a popular PHP templating library, via a server-side generated template.

React.js is a JavaScript view library that allows developers to create interfaces is a structured way based on a hierarchical component structure. React can either create the DOM structure from scratch, or attach to an existing one rendered by the server to speed up the first load.

If you create Twig templates that match the React rendering, you can take advantage of this feature without a complicated rendering setup.

While there were other methods created to try to solve the problem (rendering the React.js template on the server side) they show a better way via React.js 16 and Twig templates. Code is included showing how to create a simple React application, and how to hook in Twig via a "hydrate" call to pull in the content. React.js has a bit of an issue by default but a little extra work on the Twig side fixes that (see the post for the solution on that one).

tagged: reactjs template render twig serverside inject content tutorial

Link: https://symfony.fi/entry/attaching-react-js-to-a-template-rendered-on-the-server-with-twig

SitePoint PHP Blog:
Procedurally Generated Game Terrain with ReactJS, PHP, and Websockets
Apr 03, 2017 @ 18:41:45

Christopher Pitt has continued his series on the SitePoint PHP blog showing the creation of a game combining PHP & React JS. In this latest part of the series he builds on part one and hows how to procedurally generated game terrain.

Last time, I began telling you the story of how I wanted to make a game. I described how I set up the async PHP server, the Laravel Mix build chain, the ReactJS front-end, and the Web Sockets connecting all this together. Now, let me tell you about what happened when I starting building the game mechanics with this mix of ReactJS, PHP, and Websockets…

He started with the idea of a simple "farm" in the game represented by a set of "patches" in the game (pixels, basically). He includes the code he use to create it and allow for new ones to be created on request (including both the PHP side and the React.js code). He then shows how to modify it to make each farm requested come in with a bit of randomness as far as what kind of terrain (dirt/grass/wheat) it came in with. The post wraps up with the code required to render the farm in the UI and style it based on what kind of terrain it contained.

tagged: game terrain reactjs tutorial generated farm render procedural

Link: https://www.sitepoint.com/procedurally-generated-game-terrain-reactjs-php-websockets/

Joshua Thjissen:
Understanding Symfony2 Forms
Sep 14, 2015 @ 14:28:50

Joshua Thjissen has a post on his site that wants to help you understand the basics of Symfony2 forms including how to build them, extend them and the modules they're made up of.

To actually use Symfony2 forms, all you need to do is read some documentation, a few blog posts and you’ll be up and running in a couple of minutes. Understanding Symfony2 forms however, is a whole different ballgame. In order to understand a seemingly simple process of “adding fields to a form”, we must understand a lot of the basic foundation of the Symfony2 Form component. In these blog posts, I’ll try and give some more insights on this foundation.

He starts by explaining the three main steps in the typical form lifecycle: building the form itself, populating and validating data and rendering the form to the waiting user. He then gets into some of the basics of using forms and the types of objects that make them up. He includes examples of creating a simple form, the YAML configuration it compiles to and the functions used to build, render and set options on the form. He finishes up the post looking at form inheritance, extending the form types and where the "ResolvedFormType" comes in to play.

tagged: symfony2 form understand overview types build render validate populate

Link: https://www.adayinthelifeof.nl/2015/09/11/understanding-symfony2-forms/

SitePoint PHP Blog:
Rendering Data in Yii 2 with GridView and ListView
Aug 10, 2015 @ 15:18:16

The SitePoint PHP blog has posted a tutorial continuing their look at the features of the Yii2 framework. In this latest post author Arno Slatius covers the use of the GridView and ListView components to render tabular data.

In my previous article about Yii 2.0, I introduced the ActiveRecord implementation. Once you have some data in your database you’ll want to be able to show it. Yii uses DataProviders to interact with data sources and it provides some widgets to output the data. Of these, the ListView and GridView provide the most functionality.

In his example he shows how to render tags about authors, articles and related tags into a simple table with contents provided by a data provider. He talks about the three different types of providers, activedata, sqldata and arraydata, and briefly shows each in use. He shows how to configure the GridView element to use the provider and set up the column data. He also covers the use of the sorting and pagination functionality already built into the tool.

tagged: tutorial yii2 framework gridview listview configure dataprovider configure render

Link: http://www.sitepoint.com/rendering-data-in-yii-2-with-gridview-and-listview/

Rob Allen:
Using ZF2 Forms with Twig
Sep 23, 2014 @ 14:28:53

Rob Allen has a new post today showing how to integrate Zend Framework 2 forms into a Slim framework based application. He started the topic in a previous post and continues, this time using the Twig templating framework to handle the rendering.

The ZF2 view helpers, formRow and formElement now look like Twig functions, however we don't want to have to rewrite all our ZF2 view helpers into Twig. Fortunately, Twig supports the concept of a undefined function callback is called whenever Twig encounters a function that it doesn't know how to call. We can use this to proxy through to the ZendView system and get it to render the ZF2 view helpers.

He shows how to use this callback functionality and a custom view layer with the Slim-Views component to render the output. He includes a simple Slim example, setting up the custom View class as a parser extension and how to register the callback to invoke Twig.

tagged: zendframework twig render slimframework integration tutorial view

Link: http://akrabat.com/zend-framework-2/using-zf2-forms-with-twig/

Stoyan Stefanov:
Server-side React with PHP – part 2
Sep 19, 2013 @ 14:35:38

In a a previous post Stoyan Stefanov introduced a setup where you could render React templates on the server-side with the help of PHP and the v8 parsing. In this second part of the series, he extends that system and shows how to use it to update views based on new data.

Part 1 ended with todos. The first one was to couple the server-side generated code with the client-side React, so that any updates past the initial page load will be handled by React's client JS, which is where React shines. Let's see how you can do just that.

He gives an example similar to his previous one - displaying a table - but shows how to inject some values from PHP as a JSON string into the component. This time he saves the output of that rendering into a variable and reuses it as a part of a whole site render later.

tagged: react javascript js v8 serverside render tutorial data

Link: http://www.phpied.com/server-side-react-with-php-part-2/

Rob Allen:
Returning JSON errors in a ZF2 application
Sep 09, 2013 @ 15:49:30

Rob Allen has a quick post to his site for the Zend Framework 2 users out there showing how to return JSON errors from your requests.

If you have a standard ZF2 application and accept application/json requests in addition to application/html, then you have probably noticed that when an error happens, HTML is created, even though the client has requested JSON. One way to fix this is to create a listener on MVC's render event to detect that an error has occurred and substitute a JsonModel in place of the ViewModel. The easiest way to do this in your ApplicationModule.

He includes some example code showing how to attach a listener for rendering ("onRenderError") and the code that defines the renderer itself. It goes through a series of checks on the request and then gets the error information from the Response. Some common errors are caught and replaced with a default message, otherwise its just directly sent to render. He then attaches the whole thing in the bootstrap and shows a simple way to test the output with a cURL call.

tagged: return json zendframework2 application tutorial render

Link: http://akrabat.com/zend-framework-2/returning-json-errors-in-a-zf2-application/

Alessandro Nadalin:
Integrating Twig in Your Legacy PHP Code
Jun 18, 2013 @ 15:19:49

Alessandro Nadalin has posted a two part series to his site to help you get the Twig templating tool integrated with your legacy codebase.

It might happen that you are working on a legacy code that is years old, with its own templating mechanism1 that doesn't really allow you to take advantage of the benefits that a structured and object-oriented engine like Twig. In this situations, when a complete replacement would cost too much to your organization, you can take advantage of a wild integration between this advanced template engine and your existing code.

In the first part of the series, he proposes integrating it at the base level, essentially wrapping the "render" method of your framework of choice. In the second part of the series, though, he comes back with a "less wild" approach that also lets you take advantage of some of the other features twig has to offer. He modifies the previous example to output a template file and reuse that in the Twig "render" call.

tagged: twig legacy codebase integration template render

Link: http://odino.org/integrating-twig-in-your-legacy-php-code

Bertrand Mansion's Blog:
Twitter Bootstrap and the QuickForm2 Callback Renderer
Sep 26, 2011 @ 17:23:41

In a new post Bertrand Mansion shows how he combined the versatility of the PEAR QuickForm2 package and the Bootstrap project from Twitter to quickly make a form using the project's styling (CSS).

I don't know about you, but for me building HTML Forms and styling HTML Forms are maybe the most boring things in web development. It's repetitive and takes a lot of time to do things correctly. That's why tools like Twitter's Bootstrap and PEAR's HTML_QuickForm2 can help with this part of our job. Wouldn't it be nice to have QuickForm2 generate a markup compatible with Bootstrap CSS, so that you could get a nice looking form without to much efforts? Well, that's what I plan to do here.

He starts by creating a simple QuickForm2 form with no renderers attached (no pre-defined styles) and a custom render callback that wraps the items in "div" tags with the correct styles. There's also a custom renderer included for grouping items with additional styling attached.

tagged: twitter bootstrap pear quickform2 callback style render css

Link:


Trending Topics: