### Manual Project Setup: Start Application with Node.js
Source: https://markojs.com/docs/introduction/installation
Starts the application by running the Express server script using Node.js. This command assumes the server logic is saved in 'index.js'.
```bash
node index.js
```
--------------------------------
### Manual Project Setup: Install Express Server Dependency
Source: https://markojs.com/docs/introduction/installation
Installs the Express.js framework, which will be used to create a basic server for handling requests and rendering Marko pages.
```bash
npm install express
```
--------------------------------
### Manual Project Setup: Create Directory and Initialize Package
Source: https://markojs.com/docs/introduction/installation
Creates a new project directory and initializes a package.json file using npm. This is the first step in a manual Marko project setup.
```bash
mkdir my-marko-application
cd my-marko-application
npm init --yes
```
--------------------------------
### Install Marko Run Package (npm)
Source: https://markojs.com/docs/marko-run/getting-started
Installs the Marko Run package using npm. This is required for both new projects starting from zero and for adding Marko Run to existing Marko projects.
```sh
npm install @marko/run
```
--------------------------------
### Manual Project Setup: Install Marko and Vite Dependencies
Source: https://markojs.com/docs/introduction/installation
Installs Marko as a project dependency and Vite with its Marko plugin as development dependencies. Requires Node.js and npm.
```bash
npm install marko@next
npm install -D vite @marko/vite
```
--------------------------------
### Preview Production Build with Marko Run (npm exec)
Source: https://markojs.com/docs/marko-run/getting-started
Builds the application for production and starts a local preview server. This is useful for testing the production build before deploying.
```sh
npm exec marko-run preview
```
--------------------------------
### Initialize New Marko Project with Marko Run
Source: https://markojs.com/docs/introduction/installation
Uses npm to initialize a new Marko project with the 'basic' template and starts the development server. Requires Node.js and npm.
```bash
npm init marko -- -t basic
cd ./my-marko-application
npm run dev
```
--------------------------------
### Start Marko Run Development Server (npm exec)
Source: https://markojs.com/docs/marko-run/getting-started
Starts the Marko Run development server using npm exec. This command enables watch mode for rapid development and hot module replacement.
```sh
npm exec marko-run
```
```sh
npm exec marko-run dev
```
--------------------------------
### Manual Project Setup: First Marko Page Content
Source: https://markojs.com/docs/introduction/installation
Defines the basic HTML structure for the root Marko page ('src/routes/index.marko'). This file serves as the entry point for the web application's UI.
```marko
Hello Marko!
```
--------------------------------
### Initialize Marko Project with Template (npm)
Source: https://markojs.com/docs/marko-run/getting-started
Initializes a new Marko project using a specified template via npm. This is a common starting point for new Marko applications, often including Marko Run by default.
```sh
npm init marko -- -t basic
```
--------------------------------
### Build Production Artifacts with Marko Run (npm exec)
Source: https://markojs.com/docs/marko-run/getting-started
Creates a production-ready build of the Marko Run application. This command optimizes assets and prepares the application for deployment.
```sh
npm exec marko-run build
```
--------------------------------
### Manual Project Setup: Express Server with Vite SSR
Source: https://markojs.com/docs/introduction/installation
Sets up an Express server that integrates with Vite's Server-Side Rendering (SSR) capabilities to render Marko components. This code is intended for development environments.
```javascript
import express from "express";
import { createServer } from "vite";
const app = express();
const vite = await createServer({
server: {
middlewareMode: true,
},
appType: "custom",
});
app.use(vite.middlewares);
app.get("/", async (req, res) => {
const template = (
await vite.ssrLoadModule("./src/routes/index.marko?marko-server-entry")
).default;
const result = template.render();
res.header("Content-Type", "text/html");
result.pipe(res);
});
app.listen(3000);
```
--------------------------------
### Inline SCSS and LESS Styles in Marko
Source: https://markojs.com/docs/guide/styling
Shows how to use SCSS and LESS preprocessors with the `
```
--------------------------------
### Manual Project Setup: Vite Configuration with Marko Plugin
Source: https://markojs.com/docs/introduction/installation
Configures Vite to use the Marko plugin by creating a vite.config.ts file. This enables Marko rendering within the Vite build process.
```typescript
import { defineConfig } from "vite";
import marko from "@marko/vite";
export default defineConfig({
plugins: [marko()],
});
```
--------------------------------
### Marko.js Middleware Function Example
Source: https://markojs.com/docs/marko-run/file-based-routing
Provides an example of a '+middleware.ts' file, demonstrating how to implement middleware for request handling. This example logs request start and completion, and includes error handling using a try-catch-finally block.
```typescript
/* +middleware.ts */
export default async function (context, next) {
const requestName = `${context.request.method} ${context.url.href}`;
let success = true;
console.log(`${requestName} request started`);
try {
return await next(); // Wait for subsequent middleware, handler, and page
} catch (err) {
success = false;
throw err;
} finally {
console.log(
`${requestName} completed ${success ? "successfully" : "with errors"}`,
);
}
}
```
--------------------------------
### Basic Inline Styles in Marko
Source: https://markojs.com/docs/guide/styling
Demonstrates using the `
Hello!
```
--------------------------------
### Marko: Styling with CSS and Dynamic Styles
Source: https://markojs.com/docs/tutorial/components-and-reactivity
Shows how to add styles directly within a Marko template using the `
```
--------------------------------
### Basic HTML Template in Marko
Source: https://markojs.com/docs/tutorial/fundamentals
Marko templates are largely compatible with standard HTML. This example shows a simple HTML structure for a blog header and navigation within a Marko file.
```marko
My Blog
```
--------------------------------
### Marko Installed Tag Configuration (marko.json)
Source: https://markojs.com/docs/reference/custom-tag
An example `marko.json` file that specifies the export directory for custom tags within an installed package. This configuration tells Marko where to find the tags exposed by the library.
```json
{
"exports": "./dist/tags"
}
```
--------------------------------
### Inline CSS Modules with SCSS in Marko
Source: https://markojs.com/docs/guide/styling
Demonstrates using CSS Modules with the SCSS preprocessor by combining a Tag Variable and a file extension on the `
Hello
```
--------------------------------
### Marko.js Layout Component Example
Source: https://markojs.com/docs/marko-run/file-based-routing
An example of a '+layout.marko' file, which serves as a wrapper component for nested layouts and pages. It defines an 'Input' interface and renders its content within a main HTML structure.
```marko
/* +layout.marko */
export interface Input {
content: Marko.Body;
}
My Products
<${input.content}/> // render the page or layout here
```
--------------------------------
### Marko: Controllable `` Tag Example
Source: https://markojs.com/docs/reference/core-tag
Demonstrates how to make the `` tag controllable using the `valueChange=` attribute. This allows for interception and transformation of state updates, enabling synchronization between parent and child components.
```marko
```
--------------------------------
### Install and Use Marko Type Checker CLI
Source: https://markojs.com/docs/introduction/installation
Installs the `@marko/type-check` package as a development dependency for type checking Marko files. After installation, the `mtc` command can be used as a replacement for `tsc` to perform Marko-specific type checks.
```bash
npm install -D @marko/type-check
```
```bash
mtc
```
--------------------------------
### Inline CSS Modules in Marko
Source: https://markojs.com/docs/guide/styling
Explains how to use CSS Modules by adding a Tag Variable to the `
```
--------------------------------
### Core Marko To-Do List Example
Source: https://markojs.com/docs/explanation/nested-reactivity
A foundational Marko component demonstrating a client-side To-Do list with add and delete functionality. It uses local state for managing the list of todos and next available ID.
```marko
```
--------------------------------
### Using TypeScript Syntax in Marko Templates
Source: https://markojs.com/docs/reference/typescript
Illustrates how TypeScript expressions can be directly used within Marko templates for dynamic content rendering and type casting. It shows an example of casting an element to HTMLInputElement.
```marko
${(input.el as HTMLInputElement).value}
```
--------------------------------
### Imported Styles in Marko
Source: https://markojs.com/docs/guide/styling
Shows how to import external CSS files into a Marko template using the `import` statement. Useful for sharing styles across multiple templates. Dependencies: Bundler configuration for CSS imports.
```css
/* fancy.css */
.fancy {
color: red;
}
```
```marko
/* index.marko */
import "./fancy.css";
Hello!
```
--------------------------------
### Marko Lifecycle Tag: Integrating with WorldMap API
Source: https://markojs.com/docs/reference/core-tag
This example demonstrates using the `` tag to initialize, update, and destroy a `WorldMap` instance. It shows how to manage the map's state (coordinates, zoom) based on component data and lifecycle events.
```marko
client import { WorldMap } from "world-map-api";
onMount() {
this.map = new WorldMap(container(), { latitude, longitude, zoom });
}
onUpdate() {
this.map.setCoords(latitude, longitude);
this.map.setZoom(zoom);
}
onDestroy() {
this.map.destroy();
}
/>
```
--------------------------------
### Marko Reactive Counter Example
Source: https://markojs.com/docs/reference/reactivity
Demonstrates a simple counter using Marko's reactivity. A '' tag initializes a 'count' variable to 0. A button increments 'count', and the button's text content, which displays '${count}', automatically updates to reflect the new value.
```marko
```
--------------------------------
### Imported CSS Modules in Marko
Source: https://markojs.com/docs/guide/styling
Demonstrates importing CSS Modules files (typically named `*.module.css`) into Marko templates. Classes are accessed via an imported `styles` object. Dependencies: Bundler configured for CSS Modules.
```css
/* something.module.css */
.fancy {
color: red;
}
```
```marko
/* index.marko */
import * as styles from "./something.module.css";
```
--------------------------------
### Marko Environment Coordination: Tabs Component
Source: https://markojs.com/docs/explanation/targeted-compilation
This Marko tabs component example illustrates environment coordination. Server compilation renders initial HTML, while client compilation generates JavaScript only for interactive elements like tab switching and visibility updates.
```marko
/* tabs.marko */
// Static
${input.title}
// Interactive
// Static content dependent on interactive state
${section.content}
```
--------------------------------
### Marko Tag Type Arguments with Generics
Source: https://markojs.com/docs/reference/typescript
Shows how to explicitly provide type arguments when using a Marko tag that has generic type parameters. This example explicitly sets the type argument to 'number' for a '' tag.
```marko
/* components/child.marko */
export interface Input {
value: T;
}
```
```marko
/* index.marko */
// number would be inferred in this case, but we can be explicit
value=1 />
```
--------------------------------
### Enable TypeScript for Marko Sites/Web Apps with tsconfig.json
Source: https://markojs.com/docs/reference/typescript
To enable TypeScript support for Marko websites and web applications, a `tsconfig.json` file at the project root is the primary requirement. This configuration file guides the TypeScript compiler.
```plaintext
src/
package.json
tsconfig.json
```
--------------------------------
### Configure tsconfig.json for Marko.js
Source: https://markojs.com/docs/introduction/installation
This JSON configuration file enables TypeScript support for a Marko.js project. It specifies compiler options such as module resolution, output directory, and strict type checking. Ensure this file is placed in the project's root directory.
```json
{
"include": ["src/**/*"],
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"lib": ["dom", "ESNext"],
"module": "ESNext",
"moduleResolution": "bundler",
"noEmit": true,
"noImplicitOverride": true,
"noUnusedLocals": true,
"outDir": "dist",
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext",
"verbatimModuleSyntax": true
}
}
```
--------------------------------
### Interactive Rating Component in Marko
Source: https://markojs.com/docs/introduction/why-marko
An example of an interactive rating component built with Marko. It uses `` for state (rating, hovering) and conditional logic (``, ``) to display the rating status dynamically.
```marko
```
--------------------------------
### Auto-Discovered Styles for Marko Custom Tags
Source: https://markojs.com/docs/guide/styling
Illustrates how Marko automatically discovers and processes style files (e.g., `style.css`) located adjacent to custom tag files. Styles are imported and processed like inline styles. Input: `.css` file, Output: Global styles.
```css
/* style.css */
.fancy {
color: green;
}
```
```marko
/* index.marko */
Hello!
```
--------------------------------
### Typing Marko Input with JSDoc
Source: https://markojs.com/docs/reference/typescript
Demonstrates how to use JSDoc comments to define the types for a Marko component's input properties. This allows for type checking in JavaScript projects without a full TypeScript setup.
```marko
// @ts-check
/**
* @typedef {{
* firstName: string,
* lastName: string,
* }}
*/
${firstName} ${lastName}
```
--------------------------------
### Advanced Input Handling and Formatting in Marko
Source: https://markojs.com/docs/reference/native-tag
Presents several examples of input handling in Marko, including an uncontrolled input, a controlled input using the `:=` shorthand, and a controlled input with a custom `valueChange` handler that formats a credit card number by adding spaces.
```marko
// uncontrolled - The browser owns the state
// controlled - The `inputValue` tag variable owns the state
// controlled - Modifications to `` are transformed
```
--------------------------------
### Marko Tag Type Parameters with Generics
Source: https://markojs.com/docs/reference/typescript
Demonstrates how to define generic type parameters for a Marko tag, allowing the tag to work with different types. This example shows a '' tag with a type parameter 'T'.
```marko
|value: T|>
...
```
--------------------------------
### Manipulating Input with `valueChange` in Marko
Source: https://markojs.com/docs/reference/native-tag
Illustrates how `valueChange` can be used to intercept and transform input values before updating the component state. This example automatically converts all input characters to lowercase, showcasing its utility for input masking and validation.
```marko