Part 3: the public blog

By the end of this part, you will have:

  • a home page showing one card per published article;
  • an Article page whose address contains the article's slug;
  • a back-office restricted to authorised people.

1. The list of published articles

For a list of cards, BWEB repeats a container: it renders it once per item of a collection or an entity selection, and its child blocks read the current item.

  1. Open the domain's home page (the administration's See the site link). It was created with the domain; replace its default content with yours.
  2. Place a Container named postCards.
  3. In its parameters, turn on Repeat (vb_Repeat) and enter as the function to execute (vt_RepeatFunctionToExecute):
ds.POST.query("isPublished = :1"; True).orderBy("publicationDate desc")

On each pass, the current article goes into the vv_RepeatData process variable (the default name of the vt_RepeatKeyname setting). Inside the container, place:

BlockSettingValue
Translation (translation)4D expression (vt_TextToExecute), tag h2 (vt_Tag)vv_RepeatData.title
Translation4D expression, tag pvv_RepeatData.summary
Link (link)4D function for the link (vt_4dFunctionToGetLink)"/article/"+vv_RepeatData.slug

Inside the link, place a text block "Read the article". Adapt /article/ to the real address of the Article page created in the next step. To lay the cards out as a grid, set the parent container's layout in the Layout tab.

Only one card shows? For someone who may edit the page, a repeat only shows its first pass, unless the vb_noRepeatInDev setting ("Display repeat in dev mode") is on. Despite its name, when on it shows every pass. Visitors always see all the articles.

The repeat function is an expression evaluated by EXECUTE FORMULA: it must return a collection or an entity selection. BWEB replaces every () in the expression with (vv_Value), the render context; to call a function that takes no parameter, give it an optional one.

2. The article page

  1. In the Pages screen, add an Article page and tick Published.
  2. In its URL parameters, add a postSlug parameter: table POST, field slug, process variable Entity.
  3. Note the generated address (for example /article/{postSlug}) and copy its beginning into the cards' link.

When a visitor opens /article/test-article-1, BWEB looks in POST for the article whose slug is test-article-1 and puts it into Entity. An unknown slug gives a 404 page.

Then compose the page:

  1. Place a postDetail container. In its Conditional display, enter Entity.isPublished and pick the Not loaded effect: a draft is not shown to visitors.
  2. Inside the container, place three Translation blocks whose 4D expression is:
    • Entity.title, tag h1;
    • String(Entity.publicationDate), tag p;
    • Entity.content, with no tag: the content is already HTML.

The Translation block inserts the result of its expression as is, as HTML. Keep it for content typed by your editors, and sanitised: that is the job of BSPK_RTE_DECODE in savePost for the rich-text content. Never display a value typed by a visitor this way.

The slug computed by savePost is deliberately simplified: two articles with the same title would get the same address, and the first one found would be shown. In a real application, make the slug unique.

3. Protect the back-office

For now, the Articles and Article editing pages answer anyone who knows their address. Before putting the site online, restrict them to your editors:

  • a page carries a view right (userRightUuid) and an edit right (editRightUuid); a visitor without the view right cannot open it;
  • rights are granted to user groups, from the administration's Groups screen;
  • a page's events are checked against that page's rights: a back-office button does not run for someone who may not view the page.

The full picture is in Users and rights.

4. See the site as a visitor

Logged in as DEV, you see the dev panel, hidden blocks (flagged with a badge) and unpublished pages. To see the site as a visitor, log out (/bweb/logout) or open a private browsing window, then browse the home page and the articles.

Going further

  • A menu shared by every page: the domain's header, footer and side bars are composed in their own administration screens (Header, Left side bar…). See The admin screens.
  • Translated messages: replace the texts hard-coded in savePost with translation keys read through BSPK_Translate. See Translations.
  • A search in the article list: the listbox has its own search zone. See The listbox.
  • Per-row actions (deleting from the list, for example): these are column events. See The listbox.
  • Navigation without reloading between the blog's pages: see SPA navigation.
  • Reusable blocks (an article card, for example): see Models and slots.

See also