0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

มุมมอง แม่แบบ และพื้นฐาน LiveView

แสดงเนื้อหาแบบไดนามิกด้วยมุมมองและแม่แบบของ Phoenix พร้อมทำความรู้จักพลังของ Phoenix LiveView

มุมมอง แม่แบบ และพื้นฐาน LiveView เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Unlocking Dynamic Content

Web applications aren't static! You need ways to display changing data, like user profiles or product lists. This is where views and templates come in.

They work together to transform raw data from your application into the beautiful HTML that users see in their browsers.

Views: Your Data's Designer

In Phoenix, a View is an Elixir module responsible for preparing data before it's rendered into a template. Think of it as a middleman between your controller and the final HTML.

Views can:

  • Format dates or numbers.
  • Create dynamic links or URLs.
  • Transform complex data structures into simpler ones for display.

Templates: Crafting HTML

Templates are files (often with a .eex extension or using the ~H sigil) that combine standard HTML with embedded Elixir code. Elixir's templating engine, EEx (Embedded Elixir), allows you to inject dynamic content.

  • <%= expression %>: Evaluates an Elixir expression and outputs its result directly into the HTML.
  • <% expression %>: Evaluates an Elixir expression but does NOT output its result. Useful for control flow like loops or conditionals.

The Rendering Flow in Action

Here's a simplified look at how Phoenix renders a page:

  1. A Controller action receives an HTTP request.
  2. The Controller calls render/3 (e.g., render(conn, "index.html", assigns)), passing data as 'assigns'.
  3. The corresponding View module (e.g., PageView) receives these assigns and can apply helper functions.
  4. The Template file (e.g., index.html.eex) uses the prepared assigns to generate the final HTML.
  5. This HTML is then sent back to the user's browser.

EEx in Action: Dynamic Greeting

Let's see how EEx processes dynamic data. This Elixir script simulates a template rendering a value passed to it.

defmodule MyTemplateRenderer do
  def render_greeting(name) do
    template_string = "<h1>Hello, <%= name %>!</h1>"
    EEx.eval_string(template_string, [name: name])
  end
end

IO.puts MyTemplateRenderer.render_greeting("CoddyKit Learner")

Introducing Phoenix LiveView

Imagine building highly interactive web interfaces without writing a single line of JavaScript. That's the magic of Phoenix LiveView!

LiveView allows you to render dynamic, real-time user interfaces entirely on the server, using WebSockets for communication. It brings the power of server-side Elixir directly to your frontend interactions.

LiveView's Core: `mount` & `render`

Every Phoenix LiveView module typically defines at least two key functions:

  • mount/3: This function is called when the LiveView is first initialized. It's used to set up the initial state (known as 'assigns') of your LiveView.
  • render/1: Takes the current assigns and returns a special HEEx template (using the ~H sigil) to generate the HTML. This function is called automatically whenever the LiveView's state changes.

Adding Interaction: Events

LiveView handles user events (like clicks or form submissions) by sending messages over the WebSocket back to the server. You attach special phx- attributes to your HTML elements to trigger these events.

  • phx-click="event_name": Triggers a server-side event when the element is clicked.
  • handle_event/3: A callback function in your LiveView module that receives and processes these events, updating the LiveView's state (assigns).

Simple LiveView Counter Concept

Here's a simplified look at a LiveView that implements a basic counter. Notice how handle_event updates the @count assign, which automatically re-renders the template.

defmodule MyAppWeb.CounterLive do
  use Phoenix.LiveView, layout: {MyAppWeb.LayoutView, :live}

  def mount(_params, _session, socket) do
    {:ok, assign(socket, :count, 0)}
  end

  def render(assigns) do
    ~H"""
    <h1>Count: <%= @count %></h1>
    <button phx-click="increment">Increment</button>
    """
  end

  def handle_event("increment", _value, socket) do
    {:noreply, update(socket, :count, &(&1 + 1))}
  end
end

Check Your Understanding

In Phoenix, what is the primary purpose of a View module?

Recap: Views, Templates, LiveView

You've learned how Phoenix uses Views to prepare data and Templates (EEx/HEEx) to render dynamic HTML. We also introduced Phoenix LiveView, a powerful tool for building interactive UIs with Elixir, minimizing the need for client-side JavaScript.

Understanding these components is key to building dynamic and engaging web applications with Phoenix. Next, you might explore more advanced LiveView features or dive into data persistence with Ecto!

คำถามที่พบบ่อย

บทเรียน “มุมมอง แม่แบบ และพื้นฐาน LiveView” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “มุมมอง แม่แบบ และพื้นฐาน LiveView” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “มุมมอง แม่แบบ และพื้นฐาน LiveView”

แสดงเนื้อหาแบบไดนามิกด้วยมุมมองและแม่แบบของ Phoenix พร้อมทำความรู้จักพลังของ Phoenix LiveView คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “มุมมอง แม่แบบ และพื้นฐาน LiveView” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตั้งค่าและโครงสร้างโครงงาน Phoenix
  2. การกำหนดเส้นทาง ตัวควบคุม และ Plugs
  3. มุมมอง แม่แบบ และพื้นฐาน LiveView
  4. ความสามารถแบบเรียลไทม์ด้วย Phoenix Channels
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development