Mandarine.TS
Latest
Latest
  • Mandarine.TS
  • Getting started
    • Installing Deno Runtime
    • Setting up Environment
    • Quick Example
  • Concepts
  • Mandarine Project Structure
  • Mandarine CLI
    • CLI Reference
      • mandarine new
      • mandarine generate
      • mandarine run
  • Mandarine Core
    • Core Initialization
    • Mandarine.json
    • Dot env file
    • Properties
      • The @Value Decorator
    • Components
      • Controller
      • Middleware
      • Repository
      • Component
      • Service
      • Configuration
      • Manual Component
    • Dependency Injection
      • Accessing DI container
    • Resource Handlers
      • Resource Handler Registry
      • Resource Handler
      • Resource Resolver
  • Mandarine MVC
    • Web MVC
    • Controllers
      • Routes
        • Parameterized Routes
      • HTTP Handlers
      • @ResponseStatus
    • Custom Middleware
    • Session Middleware
    • CORS Middleware
    • Template Engine
      • @Render Decorator
      • Accessing data from template
    • Launching web-app
    • Serving Static Content
    • Multipart Form Data
  • Mandarine Security
    • Sessions
  • Mandarine Data
    • ORM
      • Data source
      • Models
      • Repositories
        • Interacting with data
        • Updating data
    • Mandarine Query Language
  • Resources
    • Changelog
  • Plugins
    • Optional
    • Promise Repeater
Powered by GitBook
On this page
  • Main
  • The @Model decorator
  • Usage

Was this helpful?

  1. Mandarine MVC
  2. Template Engine

Accessing data from template

This page summarizes all the concepts needed to access data from a template

Previous@Render DecoratorNextLaunching web-app

Last updated 4 years ago

Was this helpful?

Main

Mandarine's engine allows you to interact and make your templates access data located in your application's back-end. For this, your decorated with must return an object. This object will then be accessible from the template & it is meant to have the information you would like to interact with.

The @Model decorator

The @Model decorator provides a data modeler for your template in a programatic way. It is a , which means, it is used & injected in the parameters of an HTTP handler.

@Model working as your data modeler for your template makes your code easier to interact with & more readable. Although, it does not have any important functionality more than serving as a helper for your template's data.

When using (object injected from @Model), your method instead of returning an object, must return this class as Mandarine will resolve the attributes added to the modeler at request time.

Syntax:

@Model()

Usage

Example

./src/main/resources/templates/my-template.html

<h2>Hello <%= data.name %> <%= data.lastname %></h2>
<small>Your current address is: <%= data.address.city %>, <%= data.address.state %>, <%= data.address.country %>. </small>

import { Controller, GET, Render, Model, ViewModel, MandarineCore } from "https://deno.land/x/mandarinets/mod.ts";

@Controller()
export class MyController {

    @GET('/path-template')
    @Render('my-template.html')
    public httpHandler(@Model() model: ViewModel) {
    
        model.setAttribute("data", {
            name: "Andres",
            lastname: "Pirela",
            address: {
                city: "New york",
                state: "NY",
                country: "United States"
            }
        });

        // returning ViewModel object.
        return model;
    }
    
    @GET('/manual-template')
    @Render(`<h2><%= name %></h2>`, { manual: true })
    public httpHandler2() {
        return {
            name: "Andres"
        };
    }
    
}

new MandarineCore().MVC().run();
HTTP Handler
@Render
HTTP Parameter Decorator
ViewModel
http://127.0.0.1:8080/path-template
http://127.0.0.1:8080/manual-template