Node Combined with SWIG rendering tattoo

How to apply the SWIG touchpad here, summarize some basic usage.

Of course, it is of course using the Express framework to build services on the Node background

 VAR Express = Require ('Express'); var Server = express (); Server.Listen (8080, 'localhost', (Req, res) => {Console.log ('server start ...');}   
After starting success, start setting up the configuration of the SWIG touchpad, the specific code is as follows:


NPM INSTALL SWIG -S
   
After the installation is successful, coupled with SWIG configuration, the code is as follows:

// Configure the touchpad engine var swig = request (‘swig’); / / Parameter 1, the name of the touchpad engine, the fixed field // parameter 2, the method of touching the board engine server.Engine (‘html’, swig.renderfile; // Touch the board engine storage directory keyword, fixed field //// The actually existing directory, the HTML file is in the HTML folder below Server.Set (‘Views’, __ DIRNAME + ‘/ HTML’); / / Register Touch Board Engine, fixed field server.set (‘View Engine’, ‘HTML’) ; // Turn off the SWIG cache, the purpose of the cache is also the response speed of the Node server swig.setdefaults ({Cache: false});

 Configure data that requires rendering:   
Server.get (‘/’, (REQ, RES) => {// render method only after using the touch board engine, where the parameter 1 is the name of the touch board that needs to be rendered. Parameter 2 is a number of parameters that need to be rendered to Res.Render (‘Temp’, {Name: ‘Zhang 3’, User: {Name: ‘Chestnut’, Age: 18}, Lists: [‘Item1’, ‘Item2’ , ‘item3’, ‘item4’, ‘item5’, ‘item6’, ‘item7’, ‘item8’, ‘item9’, ‘Item 10’, ‘Item11’, ‘Item12’, ‘Item13’, ‘Item 14’, ‘ Item15 ‘,’ Item16 ‘,’ Item17 ‘,’ Items20 ‘,’ Item21 ‘,’ Item22 ‘,’ Item23 ‘,’ Item24 ‘,’ Item25 ‘,’ Item26 ‘]}); })

The specific code of the HTML file is shown below:
   

Document
 Topping Board Engine   Name:  {Name}  {% IF User.name == 'Chestnut' && user.age == '18'%} Name:  Chestnut  {% elseif user.name == 'Zhang 3'%}  Zhang San  {% endIF%}  Traverse array  {% fOr items in lists%}  {% EndFor%} 
{% set arr = [1, 2, 3, 4, 5]%}
{arr.length}}

The specific page is shown below:

  • What is said to use the SWIG touch board engine how to transfer parameters to page rendering, let's take a look at how to use SWIG to extract HTML public parts:

    HTML page public Part, such as Header, public JS CSS file, navigation bar, etc. set a public page:

    Node node结合swig渲染摸板的方法

    {% endblock%}



    Home

    About Us
      
    2 4
    5

    6
     {% endblock%}   {% endblock%}  
  • When the Node server is started, the home page is rendered. You will see
  • Server.get ('/', (REQ, RES) => {Res.Render ('www / home ", {})
  • The above is all the contents of this article, I hope that everyone will help, I hope everyone supports TUMI clouds. .
  • © Copyright Notice
    THE END
    Just support it if you like
    like0
    share
    comment Grab the couch

    Please log in to comment