PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://gitlab.com/learn-co-curriculum/cssi-6-4-gae-intro-to-templates
Markdown | 121 lines | 90 code | 31 blank | 0 comment | 0 complexity | fb215d32878072dbfe5a85604efff068 MD5 | raw file
  1. #Google App Engine: Intro to Templates
  2. The **template** is the view of the MVC model. This is where our python is embedded into our HTML.
  3. There are many templating systems for Python: <a href="https://docs.djangoproject.com/en/dev/topics/templates/Django">Django</a>, and <a href="http://jinja.pocoo.org/">Jinja</a> are just a few. You can use your template engine of choice by bundling it with your application code.
  4. Let's start with a simple page:
  5. **Code Along: Templating**
  6. + Clone the repo into the development directory of your local machine, or you can use the Boilerplate Code below.
  7. + Copy and save this into main.py
  8. ```python
  9. import webapp2 #webapp2 is a module that you import
  10. class MainHandler(webapp2.RequestHandler):#This is the handler class
  11. def get(self):
  12. self.response.write('Hello webapp2 world!')#replacing your print statements
  13. class CountHandler(webapp2.RequestHandler):
  14. def get(self):
  15. for i in range(1, 101):
  16. self.response.write(i)
  17. app = webapp2.WSGIApplication([
  18. ('/', MainHandler),
  19. ('/count', CountHandler),
  20. ], debug=True) #creates a WSGIApplication and assigns it to the variable app. app.yaml is pointed to this object
  21. ```
  22. + Now we need to tell <kbd>app.yaml</kbd> where to find our app (<kbd>helloworld.app</kbd>) and the <kbd>webapp2</kbd> library.
  23. ```python
  24. application: appengine-practice
  25. version: 1
  26. runtime: python27
  27. api_version: 1
  28. threadsafe: true
  29. handlers:
  30. - url: /favicon\.ico
  31. static_files: favicon.ico
  32. upload: favicon\.ico
  33. - url: /.*
  34. script: main.app
  35. libraries:
  36. - name: webapp2
  37. version: "2.5.2"
  38. ```
  39. + Run your app using the AppEngineLauncher by Adding and Existing Application, and then open your page in the browser.
  40. #Templating
  41. We can add elements and styling to our page by using **templates**.
  42. **Template** - reusable code that provides a framework for embedding python into HTML. It lets us write an html page that will have similar structure and style, but different data depending on who visits a page and the state of the application.
  43. Here is some html in a file called hello.html, in a directory called templates, saved in the same directory as your appengine-practice project. It gives us a webpage that contains two lines of text and an image.
  44. ```html
  45. <!DOCTYPE html>
  46. <html>
  47. <head>
  48. <title>Howdy</title>
  49. </head>
  50. <body>
  51. <h1>Hello World!</h1>
  52. <img height="100px" width="150px" src="https://blackgeoscientists.files.wordpress.com/2014/06/helloworld.jpg" alt="A cute Pic of a Dude on the World">
  53. <h2>How are ya?</h2>
  54. </body>
  55. </html>
  56. ```
  57. We can edit the app.yaml file to use the jinja library: jinja is a templating system for python.
  58. + If you modify the libraries section at the bottom of your app.yaml page to add jinja.
  59. ```python
  60. application: appengine-practice #Unique application name
  61. version: 1 # version 1 of this application’s code
  62. runtime: python27 # running on python 2.7
  63. api_version: 1 # API version 1
  64. threadsafe: true # how AppEngine processes multiple requests simultaneously
  65. handlers:
  66. - url: /favicon\.ico
  67. static_files: favicon.ico
  68. upload: favicon\.ico
  69. - url: .*
  70. script: main.app
  71. libraries:
  72. - name: jinja2
  73. version: latest
  74. - name: webapp2
  75. version: "2.5.2"
  76. ```
  77. + And Edit main.py to add jinja at the top of the page, plus add jinja to the handler in main.py
  78. ```python
  79. import jinja2
  80. import os
  81. import webapp2
  82. jinja_environment = jinja2.Environment(
  83. loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))#this little bit sets jinja's relative directory to match the directory name(dirname) of the current __file__, in this case, helloworld.py
  84. class MainHandler(webapp2.RequestHandler):
  85. def get(self):
  86. template = jinja_environment.get_template('templates/hello.html')
  87. self.response.out.write(template.render())
  88. ```
  89. + You can run the app and, Hooray we have styling!
  90. <p data-visibility='hidden'>View <a href='https://learn.co/lessons/cssi-6.4-gae-intro-to-templates' title='Google App Engine: Intro to Templates'>Google App Engine: Intro to Templates</a> on Learn.co and start learning to code for free.</p>