### Running the Basic FINN Application (nfinn) Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet provides instructions to set up and run the basic FINN application example. It installs necessary dependencies using npm and then starts the Node.js server, making the application accessible. ```Shell npm install ``` ```Shell node server.js ``` -------------------------------- ### Starting the Lifestyle API Proxy Server Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet outlines the steps to set up and launch the lifestyle API proxy server. It first installs required packages and then starts the Node.js server, making the API available on port 3031. The server also supports a '--help' option for configuration details. ```Shell npm install ``` ```Shell node lifestyleproxy.js ``` -------------------------------- ### Installing the Lifestyle FINN API Client Source: https://github.com/finn-no/lifestyle/blob/master/README.md This command installs the `lifestyle` package, a client for the FINN API, using npm. It adds the package to your project's dependencies. ```bash $ npm install finn-no/lifestyle ``` -------------------------------- ### Running nfinn with an API Key Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet demonstrates how to run the basic FINN application example when an API key is required. The key is passed as a command-line argument to the Node.js server for authentication. ```Shell node server.js my-key-here ``` -------------------------------- ### Combining Search and Ad Retrieval for Detailed Information in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This advanced example demonstrates chaining promises to first search for the cheapest 'selveier' apartment in Gamlebyen, then retrieve the full ad details for the first result. It logs the ad title, alternate link, and processes general text, handling potential errors. ```javascript client .search('realestate-homes', { location: '1.20061.20512', ownership_type: 3, sort: 3 }) .then(function(result) { var adId = result.entries[0].adId; return client.getAd(adId); }) .then(function(ad) { console.log(ad.title, ad.links.alternate); ad.aData.general_text.forEach(function(e) { console.log(e.value.replace(/
/g, "\n")) }); }) .catch(function(err) { console.log("Error fetching ad:"); console.log(err.stack); }); ``` -------------------------------- ### Performing a Filtered Search with the FINN API Client in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This example demonstrates how to perform a search with additional filters. It searches for 'agriculture-tools' matching 'deere' in the free text and restricted to location '20002' (Østfold), logging the filtered results. ```javascript client .search('agriculture-tools', {q: 'deere', location: '20002'}) .then(console.log); ``` -------------------------------- ### Populating API Caches on Startup in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet shows how to proactively populate the client's internal caches by performing open searches for all available categories. It fetches all search IDs via `getSearches` and then initiates a search for each, ensuring that root, OpenSearch descriptions, and models are cached. ```javascript client .getSearches() .map(function(search) { return client.search(search.title); }); ``` -------------------------------- ### Initializing the FINN API Client in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet demonstrates how to import the `lifestyle` module and initialize a new `FinnClient` instance. The constructor requires the API root URL and an optional API key, which is necessary for requests outside the FINN network. ```javascript var lifestyle = require('lifestyle'); var client = new lifestyle.FinnClient("https://cache.api.finn.no/iad/", "my-api-key-here"); ``` -------------------------------- ### Performing a Basic Search with the FINN API Client in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet shows how to perform a basic search using the `search` method with a specified `searchId` (e.g., 'agriculture-tools'). It returns a promise for the search results, which are then logged to the console. ```javascript client .search('agriculture-tools') .then(console.log); ``` -------------------------------- ### Setting Up Template Inheritance and Macro Imports in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/result.html This snippet demonstrates how to extend a base template and import macros from another file. This establishes the foundational structure and allows for the reuse of common components and functionalities across multiple templates. ```Jinja2/Nunjucks {% import './macros.html' as macros %} {% extends 'base.html' %} ``` -------------------------------- ### Retrieving Available Searches with getSearches in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This code calls the `getSearches` method on the `FinnClient` instance, which returns a promise for an array of available search categories. The result, containing titles and URLs, is then logged to the console. ```javascript client .getSearches() .then(console.log); ``` -------------------------------- ### Calling search API Method with Parameters in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet shows the `search` method call, which takes a `searchId` and an optional `params` map. It returns a promise for the search results, allowing for filtered and sorted queries based on available OpenSearch descriptions. ```javascript client.search(searchId, params); ``` -------------------------------- ### Calling getSearchDescription API Method in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet demonstrates the `getSearchDescription` method, which accepts a `searchId` (from `getSearches` results) and returns a promise for an OpenSearch search description document. This document provides details about available filters and sorters for a given search category. ```javascript client.getSearchDescription(searchId); ``` -------------------------------- ### Iterating and Displaying Search Results - Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/index.html This snippet iterates through a list of 'searches' using a Jinja2 'for' loop. For each 'search' object, it generates a numbered list item, displaying the search's title as a hyperlink to its corresponding detail page, constructed using the search's ID. ```Jinja2 {% for search in searches %}1. [{{ search.title }}](/search/{{ search.id }}) {% endfor %} ``` -------------------------------- ### Defining Template Inheritance and Content Block - Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/index.html This snippet establishes the page's structure by extending a 'base.html' template and defining a 'content' block where specific page content will be rendered. This promotes reusability and consistent layout. ```Jinja2 {% extends 'base.html' %} {% block content %} ``` -------------------------------- ### Parsing Ad XML String Directly with Lifestyle Parsers in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet demonstrates how to use the `lifestyle` package's built-in parsers directly, without the client instance. It shows parsing an XML string representing an ad (`adXmlString`) into a structured JavaScript object using `lifestyle.parsers.ad.parse`. ```javascript var lifestyle = require('lifestyle'); var ad = lifestyle.parsers.ad.parse(adXmlString); ``` -------------------------------- ### Calling getAd API Method in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet illustrates the `getAd` method call, which takes an `adId` as a parameter and returns a promise for a detailed ad object. It's used to retrieve full information for a specific advertisement. ```javascript client.getAd(adId); ``` -------------------------------- ### Rendering Lifestyle Ad Details in Jinja2 Template Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/ad.html This Jinja2 template extends a base HTML layout to display the title, the first image, and a JSON summary of an advertisement object. It utilizes template inheritance, variable interpolation (e.g., `{{ ad.title }}`), and filters (e.g., `|jsonSummary|safe`) to render dynamic content securely. ```Jinja2 {% extends 'base.html' %} {% block title %}{{ ad.title }}{% endblock %} {% block content %} {{ ad.title }} -------------- ![]({{ ad.images[0].url }}) {{ ad|jsonSummary|safe }} {% endblock %} ``` -------------------------------- ### Importing Macros in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet demonstrates how to import macros from another template file, making reusable components available within the current template. The `as` keyword assigns an alias to the imported macro file for easier access. ```Jinja2 {% import './macros.html' as macros %} ``` -------------------------------- ### Structuring Content Block with Search Results Loop in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/result.html This snippet defines the main 'content' block, responsible for displaying the subtitle and iterating through a collection of search 'hits'. For each hit, it applies a 'jsonSummary' filter and marks the output as safe, ensuring that rich, pre-formatted content is rendered correctly without unintended escaping. ```Jinja2/Nunjucks {% block content %} {{ subtitle }} {% for hit in hits %} {{ hit|jsonSummary|safe }} {% endfor %} {% endblock %} ``` -------------------------------- ### Extending Base Template in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet shows how to extend a base template, inheriting its structure and allowing specific blocks to be overridden or appended. This promotes code reuse and consistent page layouts across a website. ```Jinja2 {% extends 'base.html' %} ``` -------------------------------- ### Calling getSearches API Method in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet shows the direct call to the `getSearches` method, which returns a promise for an array of objects representing available search categories. This method is typically used to discover valid search IDs for further API queries. ```javascript client.getSearches(); ``` -------------------------------- ### Retrieving a Specific Ad by ID in JavaScript Source: https://github.com/finn-no/lifestyle/blob/master/README.md This snippet uses the `getAd` method to fetch a specific advertisement by its unique `adId` (e.g., 52403704). The method returns a promise for the ad object, which is then logged to the console. ```javascript client .getAd(52403704) .then(console.log); ``` -------------------------------- ### Defining Main Content Block - Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/base.html This Jinja2 block defines the main content area of the page. Child templates are expected to populate this block with specific page content. ```Jinja2 {% block content %}{% endblock %} ``` -------------------------------- ### Iterating Over Filters in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet shows a `for` loop iterating over a collection named `filters`. For each item, it renders a macro and applies a filter to another variable, demonstrating dynamic content generation and macro usage within a loop. ```Jinja2 {% for filter in filters %} {{ macros.filter(filter) }} {{ filter|jsonSummary|safe }} * * * {% endfor %} ``` -------------------------------- ### Defining Content Block in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet defines the main 'content' block where the primary page content will be rendered. Child templates will typically fill this block with their specific HTML, allowing for modular page design. ```Jinja2 {% block content %} ``` -------------------------------- ### Rendering Taxonomy Filters - Twig/Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/macros.html This macro initiates the rendering of a hierarchical taxonomy filter by calling the 'taxonomyLevel' macro with the top-level children of the taxonomy definition. It's the entry point for displaying nested category structures. ```Twig {% macro taxonomyFilter(filter) %} {{ taxonomyLevel(filter.definition.taxonomy.children) }} {% endmacro %} ``` -------------------------------- ### Rendering Dynamic Filters - Twig/Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/macros.html This macro acts as a dispatcher, rendering different filter types (range, select, or taxonomy) based on the 'f.type' property of the input filter object. It centralizes the logic for displaying various filter UIs. ```Twig {% macro filter(f) %} {{ f.title }} {% if f.type == 'range' %} {{ rangeFilter(f) }} {% elif (f.type == 'select') %} {{ selectFilter(f) }} {% else %} {{ taxonomyFilter(f) }} {% endif %} {% endmacro %} ``` -------------------------------- ### Defining Page Title Block - Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/base.html This Jinja2 block defines a placeholder for the page's title, allowing child templates to override or extend it. It defaults to 'By Team Lifestyle!'. ```Jinja2 {% block title %}By Team Lifestyle!{% endblock %} ``` -------------------------------- ### Defining Page Title Block in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/result.html This snippet defines the 'title' block within the template, dynamically setting the page's title. It interpolates variables for the main title and the total number of search results, which is crucial for both user experience and search engine optimization. ```Jinja2/Nunjucks {% block title %}{{ title }} - {{ totalResults }} hits{% endblock %} ``` -------------------------------- ### Displaying Variable in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet demonstrates how to display the value of a variable within the template using double curly braces. Here, it outputs the `title` variable, which is typically passed from the backend context. ```Jinja2 {{ title }} ``` -------------------------------- ### Closing Content Block - Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/index.html This snippet explicitly closes the 'content' block that was opened earlier in the template. It signifies the end of the dynamic content section for this specific page, ensuring proper template rendering. ```Jinja2 {% endblock %} ``` -------------------------------- ### Rendering Range Filters - Twig/Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/macros.html This macro is responsible for rendering a range-type filter. It currently provides a basic structure for displaying range inputs, typically used for numerical or date ranges. ```Twig {% macro rangeFilter(f) %} - {% endmacro %} ``` -------------------------------- ### Rendering Taxonomy Level - Twig/Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/macros.html This macro iterates through a collection of taxonomy children and renders each child using the 'taxonomyItem' macro. It's used to build out a single level of a hierarchical taxonomy display. ```Twig {% macro taxonomyLevel(children) %} {% for child in children %}1. {{ taxonomyItem(child) }} {% endfor %} {% endmacro %} ``` -------------------------------- ### Defining Title Block in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet defines a 'title' block, which can be overridden by child templates or filled with dynamic content. It uses the `{{ title }}` variable to display the page title, demonstrating variable interpolation. ```Jinja2 {% block title %}{{ title }}{% endblock %} ``` -------------------------------- ### Rendering Select Filters - Twig/Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/macros.html This macro iterates through the children of a select filter's taxonomy definition and renders each item's title. It's used for displaying a list of selectable options, often in a dropdown or checkbox group. ```Twig {% macro selectFilter(filter) %} {% for item in filter.definition.taxonomy.children %}1. {{ item.title }} {% endfor %} {% endmacro %} ``` -------------------------------- ### Closing Content Block in Jinja2/Nunjucks Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/search.html This snippet marks the end of the 'content' block, signifying that all content intended for this block has been defined. It's essential for proper template inheritance and structure. ```Jinja2 {% endblock %} ``` -------------------------------- ### Rendering Individual Taxonomy Item - Twig/Jinja2 Source: https://github.com/finn-no/lifestyle/blob/master/examples/nfinn/views/macros.html This macro renders an individual item within a taxonomy tree. If the item has children, it recursively calls 'taxonomyLevel' to display the nested structure; otherwise, it just displays the item's title. ```Twig {% macro taxonomyItem(branch) %} {% if branch.children %} {{ branch.title }} {{ taxonomyLevel(branch.children) }} {% else %} {{ branch.title }} {% endif %} {% endmacro %} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.