Let Nutz support the fastest template engine smarty4j

Source: Internet
Author: User
Tags php template

SMARTY4J is an open-source template engine. Yes, it is the Java porting version of the famous PHP template engine.
It is characterized by the compilation of template files or strings into a Java class directly executed, so efficiency than the general template interpretation of the way to handle faster. It developed late, so no velocity, Freemarker famous, popular also than the PHP version of the template engine much worse.
But it's fast!
In a skeptical manner, I measured it myself. When rendering 1000 times a simple page (introducing only a few simple data types), the smarty4j takes 16 milliseconds, the velocity is 63 milliseconds, and the Freemarker uses 109 milliseconds. When rendered 5,000 times, the smarty4j takes 172 milliseconds, velocity is 328 milliseconds, and Freemarker uses 390 milliseconds. (The above tests are averaged over multiple tests)
Complex pages take more time-consuming differences. So replacing the velocity or freemarker in the project with SMARTY4J can speed up your application.

Already has the Netizen to provide the smarty4j the plug-in integration into the struts2. So can you integrate it into a homegrown compact Nutz MVC framework?
After looking through the Nutz user manual, I found this to be a very easy thing to do.

Let's start here:

1, first to implement the view adapter
Very simple:

Java code
  1. /**
  2. * SMARTY4J View Adapter
  3. * @author Qinerg ([email protected])
  4. */
  5. Public class Smartyviewmaker implements Viewmaker {
  6. Public View make (IOC, string type, String value) {
  7. if ("St". Equalsignorecase (Type)) {
  8. return new Smartyview (value);
  9. }
  10. return null;
  11. }
  12. }



2. Then implement the specific view parser

Java code
  1. /**
  2. * Generate pages using smarty4j templates
  3. * @author Qinerg ([email protected])
  4. */
  5. Public class Smartyview extends Abstractpathview implements View {
  6. Private Final String ext = ". html";
  7. private static engine engine = new engine (); Loading the template engine
  8. Public Smartyview (String dest) {
  9. super (dest);
  10. Engine.settemplatepath ("");
  11. Engine.setdebug (true);
  12. }
  13. / * Render page
  14. * @see Org.nutz.mvc.view#render (javax.servlet.http.HttpServletRequest, Javax.servlet.http.HttpServletResponse, Java.lang.Object)
  15. */
  16. public void Render (HttpServletRequest req, HttpServletResponse resp,
  17. Object obj) throws Throwable {
  18. if ("". Equals (Engine.gettemplatepath ())) {
  19. String Realpath = Req.getsession (). Getservletcontext (). Getrealpath ("/");
  20. Engine.settemplatepath (Realpath);
  21. }
  22. String Path = Evalpath (req, obj);
  23. //empty path, with default rules
  24. if (Strings.isblank (path)) {
  25. Path = Mvcs.getrequestpath (req);
  26. Path = "Web-inf"
  27. + (Path.startswith ("/")? "" : "/")  
  28. + files.renamesuffix (path, ext);
  29. }
  30. //absolute path: The path beginning with '/' does not increase '/web-inf '
  31. Else if (Path.charat (0) = = '/') {
  32. if (!path.tolowercase (). EndsWith (EXT))
  33. Path + = ext;
  34. }
  35. //path in the form of package name
  36. else {
  37. Path = "web-inf/" + path.replace ('. ', '/') + ext;
  38. }
  39. Template template = engine.gettemplate (path);
  40. Context CTX = new Context (); //Generate Data container object
  41. Ctx.set ("obj", obj);
  42. Ctx.set ("Request", req);
  43. Ctx.set ("response", resp);
  44. Ctx.set ("Session", Req.getsession ());
  45. Template.merge (CTX, Resp.getwriter ());
  46. }
  47. }



All right, finish it!

So how do you use it specifically?
First declare the use of the SMARTY4J adapter on the main module

Java code
    1. @Views ({smartyviewmaker. Class})
    2. Public class Mainmodule {}



You can then declare the template path on the action, such as:

Java code
    1. @At ("/index")
    2. @Ok ("St:st.index")
    3. public Void Index () {


The template for this action will correspond to the web-inf/st/index.html file.

Of course the template path can also be placed outside the web-inf, such as:
@Ok ("ST:ABC.BBC") or @Ok ("ST:/ABC/BBC")
The corresponding template path is:
Abc/bbc.html

Let Nutz support the fastest template engine smarty4j

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.